Skip to content

Commit

Permalink
Added the spectral clock for tracking the zap apple tree's season and…
Browse files Browse the repository at this point in the history
… make the zap apple tree cyce faster
  • Loading branch information
Sollace committed Feb 1, 2024
1 parent e1f4d77 commit 8804fa0
Show file tree
Hide file tree
Showing 51 changed files with 264 additions and 28 deletions.
Binary file added assets/spectral_clock_0.xcf
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ static void bootstrap() {
PolearmRenderer.register(UItems.WOODEN_POLEARM, UItems.STONE_POLEARM, UItems.IRON_POLEARM, UItems.GOLDEN_POLEARM, UItems.DIAMOND_POLEARM, UItems.NETHERITE_POLEARM);
ModelPredicateProviderRegistry.register(UItems.GEMSTONE, new Identifier("affinity"), (stack, world, entity, seed) -> EnchantableItem.isEnchanted(stack) ? EnchantableItem.getSpellKey(stack).getAffinity().getAlignment() : 0);
ModelPredicateProviderRegistry.register(UItems.ROCK_CANDY, new Identifier("count"), (stack, world, entity, seed) -> stack.getCount() / (float)stack.getMaxCount());
ModelPredicateProviderRegistry.register(Unicopia.id("zap_cycle"), (stack, world, entity, seed) -> UnicopiaClient.getInstance().getZapStageDelta());

ColorProviderRegistry.BLOCK.register(URenderers::getTintedBlockColor, TintedBlock.REGISTRY.stream().toArray(Block[]::new));
ColorProviderRegistry.ITEM.register((stack, i) -> getTintedBlockColor(Block.getBlockFromItem(stack.getItem()).getDefaultState(), null, null, i), TintedBlock.REGISTRY.stream().map(Block::asItem).filter(i -> i != Items.AIR).toArray(Item[]::new));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.minelittlepony.unicopia.entity.player.Pony;
import com.minelittlepony.unicopia.network.handler.ClientNetworkHandlerImpl;
import com.minelittlepony.unicopia.server.world.WeatherConditions;
import com.minelittlepony.unicopia.server.world.ZapAppleStageStore;
import com.minelittlepony.unicopia.util.Lerp;

import net.fabricmc.api.ClientModInitializer;
Expand Down Expand Up @@ -53,6 +54,9 @@ public static UnicopiaClient getInstance() {
public final Lerp tangentalSkyAngle = new Lerp(0, true);
public final Lerp skyAngle = new Lerp(0, true);

private ZapAppleStageStore.Stage zapAppleStage = ZapAppleStageStore.Stage.HIBERNATING;
private long zapStageTime;

public static Optional<PlayerCamera> getCamera() {
PlayerEntity player = MinecraftClient.getInstance().player;

Expand Down Expand Up @@ -84,6 +88,15 @@ public UnicopiaClient() {
instance = this;
}

public void setZapAppleStage(ZapAppleStageStore.Stage stage, long delta) {
zapAppleStage = stage;
zapStageTime = delta;
}

public float getZapStageDelta() {
return zapAppleStage.getCycleProgress(zapStageTime);
}

public float getSkyAngleDelta(float tickDelta) {
if (MinecraftClient.getInstance().world == null) {
return 0;
Expand Down Expand Up @@ -135,6 +148,8 @@ private void onWorldTick(ClientWorld world) {
world.setRainGradient(gradient);
world.setThunderGradient(gradient);
}

zapStageTime++;
}

private Float getTargetRainGradient(ClientWorld world, BlockPos pos, float tickDelta) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void sendUpdate(MinecraftServer server) {
dirty = false;
MsgServerResources msg = new MsgServerResources();
server.getWorlds().forEach(world -> {
Channel.SERVER_RESOURCES_SEND.sendToAllPlayers(msg, world);
Channel.SERVER_RESOURCES.sendToAllPlayers(msg, world);
});
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/minelittlepony/unicopia/item/UItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ EntityAttributes.GENERIC_KNOCKBACK_RESISTANCE, new EntityAttributeModifier(Blunt
Item PALM_BASKET = register("palm_basket", new BasketItem(AirBalloonEntity.BasketType.of(UWoodTypes.PALM_BOAT_TYPE), new Item.Settings().maxCount(1)), ItemGroups.TOOLS);

Item GIANT_BALLOON = register("giant_balloon", new HotAirBalloonItem(new Item.Settings().maxCount(1)), ItemGroups.TOOLS);
Item SPECTRAL_CLOCK = register("spectral_clock", new Item(new Item.Settings()), ItemGroups.TOOLS);

Item LIGHT_GRAY_BED_SHEETS = register(CloudBedBlock.SheetPattern.LIGHT_GRAY);
Item GRAY_BED_SHEETS = register(CloudBedBlock.SheetPattern.GRAY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.minelittlepony.unicopia.*;
import com.minelittlepony.unicopia.entity.player.Pony;
import com.minelittlepony.unicopia.server.world.UnicopiaWorldProperties;
import com.minelittlepony.unicopia.server.world.ZapAppleStageStore;
import com.sollace.fabwork.api.packets.*;

import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
Expand All @@ -29,11 +30,12 @@ public interface Channel {
S2CPacketType<MsgSpellbookStateChanged<PlayerEntity>> SERVER_SPELLBOOK_UPDATE = SimpleNetworking.serverToClient(Unicopia.id("server_spellbook_update"), MsgSpellbookStateChanged::new);
C2SPacketType<MsgSpellbookStateChanged<ServerPlayerEntity>> CLIENT_SPELLBOOK_UPDATE = SimpleNetworking.clientToServer(Unicopia.id("client_spellbook_update"), MsgSpellbookStateChanged::new);

S2CPacketType<MsgServerResources> SERVER_RESOURCES_SEND = SimpleNetworking.serverToClient(Unicopia.id("resources_send"), MsgServerResources::new);
S2CPacketType<MsgServerResources> SERVER_RESOURCES = SimpleNetworking.serverToClient(Unicopia.id("resources"), MsgServerResources::new);

S2CPacketType<MsgOtherPlayerCapabilities> SERVER_OTHER_PLAYER_CAPABILITIES = SimpleNetworking.serverToClient(Unicopia.id("other_player_capabilities"), MsgOtherPlayerCapabilities::new);
S2CPacketType<MsgPlayerAnimationChange> SERVER_PLAYER_ANIMATION_CHANGE = SimpleNetworking.serverToClient(Unicopia.id("other_player_animation_change"), MsgPlayerAnimationChange::new);
S2CPacketType<MsgSkyAngle> SERVER_SKY_ANGLE = SimpleNetworking.serverToClient(Unicopia.id("sky_angle"), MsgSkyAngle::new);
S2CPacketType<MsgZapAppleStage> SERVER_ZAP_STAGE = SimpleNetworking.serverToClient(Unicopia.id("zap_stage"), MsgZapAppleStage::new);

static void bootstrap() {
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
Expand All @@ -50,8 +52,10 @@ static void bootstrap() {
Unicopia.LOGGER.info("Setting {}'s race to {} due to host setting", handler.player.getDisplayName().getString(), Race.REGISTRY.getId(race).toString());
}
}
sender.sendPacket(SERVER_RESOURCES_SEND.id(), new MsgServerResources().toBuffer());
sender.sendPacket(SERVER_RESOURCES.id(), new MsgServerResources().toBuffer());
sender.sendPacket(SERVER_SKY_ANGLE.id(), new MsgSkyAngle(UnicopiaWorldProperties.forWorld(handler.getPlayer().getServerWorld()).getTangentalSkyAngle()).toBuffer());
ZapAppleStageStore store = ZapAppleStageStore.get(handler.player.getServerWorld());
sender.sendPacket(SERVER_ZAP_STAGE.id(), new MsgZapAppleStage(store.getStage(), store.getStageDelta()).toBuffer());
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.minelittlepony.unicopia.network;

import com.minelittlepony.unicopia.server.world.ZapAppleStageStore;
import com.sollace.fabwork.api.packets.Packet;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.network.PacketByteBuf;

public record MsgZapAppleStage (
ZapAppleStageStore.Stage stage,
long delta
) implements Packet<PlayerEntity> {

public MsgZapAppleStage(PacketByteBuf buffer) {
this(buffer.readEnumConstant(ZapAppleStageStore.Stage.class), buffer.readLong());
}

@Override
public void toBuffer(PacketByteBuf buffer) {
buffer.writeEnumConstant(stage);
buffer.writeLong(delta);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ public ClientNetworkHandlerImpl() {
Channel.SERVER_BLOCK_DESTRUCTION.receiver().addPersistentListener(this::handleBlockDestruction);
Channel.CANCEL_PLAYER_ABILITY.receiver().addPersistentListener(this::handleCancelAbility);
Channel.UNLOCK_TRAITS.receiver().addPersistentListener(this::handleUnlockTraits);
Channel.SERVER_RESOURCES_SEND.receiver().addPersistentListener(this::handleServerResources);
Channel.SERVER_RESOURCES.receiver().addPersistentListener(this::handleServerResources);
Channel.SERVER_SKY_ANGLE.receiver().addPersistentListener(this::handleSkyAngle);
Channel.SERVER_ZAP_STAGE.receiver().addPersistentListener(this::handleZapStage);
Channel.SERVER_PLAYER_ANIMATION_CHANGE.receiver().addPersistentListener(this::handlePlayerAnimation);
Channel.SERVER_REQUEST_PLAYER_LOOK.receiver().addPersistentListener(this::handleCasterLookRequest);
}
Expand Down Expand Up @@ -93,6 +94,10 @@ private void handleSkyAngle(PlayerEntity sender, MsgSkyAngle packet) {
UnicopiaClient.getInstance().tangentalSkyAngle.update(packet.tangentalSkyAngle(), 200);
}

private void handleZapStage(PlayerEntity sender, MsgZapAppleStage packet) {
UnicopiaClient.getInstance().setZapAppleStage(packet.stage(), packet.delta());
}

@SuppressWarnings("unchecked")
private void handleServerResources(PlayerEntity sender, MsgServerResources packet) {
SpellTraits.load(packet.traits());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import com.minelittlepony.unicopia.USounds;
import com.minelittlepony.unicopia.Unicopia;
import com.minelittlepony.unicopia.network.Channel;
import com.minelittlepony.unicopia.network.MsgZapAppleStage;
import com.minelittlepony.unicopia.particle.LightningBoltParticleEffect;
import com.minelittlepony.unicopia.particle.ParticleUtils;
import com.minelittlepony.unicopia.util.Tickable;
Expand All @@ -20,10 +22,13 @@
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.PersistentState;
import net.minecraft.world.World;
import net.minecraft.world.dimension.DimensionType;
import net.minecraft.world.event.GameEvent;

public class ZapAppleStageStore extends PersistentState implements Tickable {
private static final Identifier ID = Unicopia.id("zap_apple_stage");
static final long DAY_LENGTH = World.field_30969;
static final long MOON_PHASES = DimensionType.MOON_SIZES.length;

public static ZapAppleStageStore get(World world) {
return WorldOverlay.getPersistableStorage(world, ID, ZapAppleStageStore::new, ZapAppleStageStore::new);
Expand All @@ -32,16 +37,18 @@ public static ZapAppleStageStore get(World world) {
private final World world;

private Stage lastStage = Stage.HIBERNATING;
private int countdown;
private long stageDelta;
private long lastTime;

private boolean stageChanged;
private boolean playedMoonEffect;
private int nextLightningEvent = 1200;

ZapAppleStageStore(World world, NbtCompound compound) {
this(world);
lastStage = Stage.VALUES[Math.max(0, compound.getInt("stage")) % Stage.VALUES.length];
stageDelta = compound.getLong("stageDelta");
stageChanged = compound.getBoolean("stageChanged");
countdown = compound.getInt("countdown");
playedMoonEffect = compound.getBoolean("playedMoonEffect");
nextLightningEvent = compound.getInt("nextLightningEvent");
}
Expand All @@ -55,32 +62,38 @@ public void tick() {
if (!world.isDay()) {
if (nextLightningEvent > 0) {
nextLightningEvent--;
markDirty();
}

if (!stageChanged && (lastStage != Stage.HIBERNATING || (world.getMoonPhase() == 0))) {
stageChanged = true;
if (countDay()) {
lastStage = lastStage.getNext();
countdown = 1;
playedMoonEffect = false;
markDirty();
onStageChanged();
}
lastStage = lastStage.getNext();
stageDelta = 0;
playedMoonEffect = false;
sendUpdate();
}
} else if (stageChanged) {
stageChanged = false;
markDirty();
}
}

private boolean countDay() {
long timeOfDay = world.getTimeOfDay();
if (stageDelta != 0 && (timeOfDay < lastTime || timeOfDay > lastTime + 10)) {
long timeDifference = timeOfDay - lastTime;
Unicopia.LOGGER.info("Times a changing {}!", timeDifference);
while (timeDifference < 0) {
timeDifference += DAY_LENGTH;
}
stageDelta += timeDifference;
sendUpdate();
}
lastTime = timeOfDay;


stageDelta++;
markDirty();
return countdown-- <= 0;
}

protected void onStageChanged() {
//world.setRainGradient(0.5F);
protected void sendUpdate() {
Channel.SERVER_ZAP_STAGE.sendToAllPlayers(new MsgZapAppleStage(getStage(), stageDelta), world);
}

public void playMoonEffect(BlockPos pos) {
Expand Down Expand Up @@ -112,7 +125,6 @@ public void triggerLightningStrike(BlockPos pos) {

/**
* Returns true during nights that the zap apples must change their states.
* @return
*/
public boolean hasStageChanged() {
return stageChanged;
Expand All @@ -125,30 +137,60 @@ public Stage getStage() {
return lastStage;
}

public long getStageDelta() {
return stageDelta;
}

public float getStageProgress() {
return getStage().getStageProgress(getStageDelta());
}

public float getCycleProgress() {
return getStage().getCycleProgress(getStageDelta());
}

@Override
public NbtCompound writeNbt(NbtCompound compound) {
compound.putInt("stage", lastStage.ordinal());
compound.putLong("stageDelta", stageDelta);
compound.putBoolean("stageChanged", stageChanged);
compound.putInt("countdown", countdown);
compound.putBoolean("playedMoonEffect", playedMoonEffect);
compound.putInt("nextLightningEvent", nextLightningEvent);
return compound;
}

public enum Stage implements StringIdentifiable {
HIBERNATING,
GREENING,
FLOWERING,
FRUITING,
RIPE;
HIBERNATING(MOON_PHASES * DAY_LENGTH),
GREENING(DAY_LENGTH),
FLOWERING(DAY_LENGTH),
FRUITING(DAY_LENGTH),
RIPE(DAY_LENGTH);

static final long DAY_LENGTH = 24000;
static final Stage[] VALUES = values();

private final long duration;

Stage(long duration) {
this.duration = duration;
}

public Stage getNext() {
return byId((ordinal() + 1) % VALUES.length);
}

public Stage getPrevious() {
return byId(((ordinal() - 1) + VALUES.length) % VALUES.length);
}

public float getStageProgress(long time) {
return (time % duration) / (float)duration;
}

public float getCycleProgress(long time) {
float ordinal = ordinal();
return MathHelper.lerp(getStageProgress(time), ordinal, ordinal + 1) / 5F;
}

public static Stage byId(int id) {
return VALUES[MathHelper.clamp(id, 0, VALUES.length)];
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/assets/unicopia/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
"item.unicopia.palm_chest_boat": "Palm Boat with Chest",

"item.unicopia.spellbook": "Spellbook",
"item.unicopia.spectral_clock": "Spectral Clock",

"emi.category.unicopia.spellbook": "Spellbook",
"emi.category.unicopia.cloud_shaping": "Shaping",
"emi.category.unicopia.growing": "Growing",
Expand Down
29 changes: 29 additions & 0 deletions src/main/resources/assets/unicopia/models/item/spectral_clock.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"parent": "item/generated",
"textures": {
"layer0": "unicopia:item/spectral_clock_0"
},
"overrides": [
{ "predicate": { "unicopia:zap_cycle": 0.00}, "model": "unicopia:item/spectral_clock" },
{ "predicate": { "unicopia:zap_cycle": 0.05}, "model": "unicopia:item/spectral_clock_1" },
{ "predicate": { "unicopia:zap_cycle": 0.10}, "model": "unicopia:item/spectral_clock_2" },
{ "predicate": { "unicopia:zap_cycle": 0.15}, "model": "unicopia:item/spectral_clock_3" },
{ "predicate": { "unicopia:zap_cycle": 0.20}, "model": "unicopia:item/spectral_clock_greening_0" },
{ "predicate": { "unicopia:zap_cycle": 0.25}, "model": "unicopia:item/spectral_clock_greening_1" },
{ "predicate": { "unicopia:zap_cycle": 0.30}, "model": "unicopia:item/spectral_clock_greening_2" },
{ "predicate": { "unicopia:zap_cycle": 0.35}, "model": "unicopia:item/spectral_clock_greening_3" },
{ "predicate": { "unicopia:zap_cycle": 0.40}, "model": "unicopia:item/spectral_clock_flowering_0" },
{ "predicate": { "unicopia:zap_cycle": 0.45}, "model": "unicopia:item/spectral_clock_flowering_1" },
{ "predicate": { "unicopia:zap_cycle": 0.50}, "model": "unicopia:item/spectral_clock_flowering_2" },
{ "predicate": { "unicopia:zap_cycle": 0.55}, "model": "unicopia:item/spectral_clock_flowering_3" },
{ "predicate": { "unicopia:zap_cycle": 0.60}, "model": "unicopia:item/spectral_clock_fruiting_0" },
{ "predicate": { "unicopia:zap_cycle": 0.65}, "model": "unicopia:item/spectral_clock_fruiting_1" },
{ "predicate": { "unicopia:zap_cycle": 0.70}, "model": "unicopia:item/spectral_clock_fruiting_2" },
{ "predicate": { "unicopia:zap_cycle": 0.75}, "model": "unicopia:item/spectral_clock_fruiting_3" },
{ "predicate": { "unicopia:zap_cycle": 0.80}, "model": "unicopia:item/spectral_clock_ripe_0" },
{ "predicate": { "unicopia:zap_cycle": 0.85}, "model": "unicopia:item/spectral_clock_ripe_1" },
{ "predicate": { "unicopia:zap_cycle": 0.90}, "model": "unicopia:item/spectral_clock_ripe_2" },
{ "predicate": { "unicopia:zap_cycle": 0.95}, "model": "unicopia:item/spectral_clock_ripe_3" },
{ "predicate": { "unicopia:zap_cycle": 1.00}, "model": "unicopia:item/spectral_clock" }
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "item/generated",
"textures": {
"layer0": "unicopia:item/spectral_clock_1"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "item/generated",
"textures": {
"layer0": "unicopia:item/spectral_clock_2"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "item/generated",
"textures": {
"layer0": "unicopia:item/spectral_clock_3"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "item/generated",
"textures": {
"layer0": "unicopia:item/spectral_clock_flowering_0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "item/generated",
"textures": {
"layer0": "unicopia:item/spectral_clock_flowering_1"
}
}
Loading

0 comments on commit 8804fa0

Please sign in to comment.