mirror of
https://github.com/Sollace/Unicopia.git
synced 2024-11-24 05:47:59 +01:00
Clean up the spellbook logic a little
This commit is contained in:
parent
48a45e6268
commit
3565060404
4 changed files with 89 additions and 61 deletions
|
@ -20,7 +20,7 @@ public class SpellbookEntityRenderer extends LivingEntityRenderer<SpellbookEntit
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Identifier getTexture(SpellbookEntity entity) {
|
public Identifier getTexture(SpellbookEntity entity) {
|
||||||
return entity.getIsAltered() ? BLUE : NORMAL;
|
return entity.isAltered() ? BLUE : NORMAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -32,7 +32,7 @@ public class SpellbookEntityRenderer extends LivingEntityRenderer<SpellbookEntit
|
||||||
protected void setupTransforms(SpellbookEntity entity, MatrixStack matrices, float f, float g, float partialTicks) {
|
protected void setupTransforms(SpellbookEntity entity, MatrixStack matrices, float f, float g, float partialTicks) {
|
||||||
super.setupTransforms(entity, matrices, f, g + 90, partialTicks);
|
super.setupTransforms(entity, matrices, f, g + 90, partialTicks);
|
||||||
|
|
||||||
if (entity.getIsOpen()) {
|
if (entity.isOpen()) {
|
||||||
matrices.translate(-1.25F, -0.35F, 0);
|
matrices.translate(-1.25F, -0.35F, 0);
|
||||||
|
|
||||||
float floatPosition = MathHelper.sin((entity.age + partialTicks + entity.getId()) / 20) * 0.04F;
|
float floatPosition = MathHelper.sin((entity.age + partialTicks + entity.getId()) / 20) * 0.04F;
|
||||||
|
|
|
@ -43,7 +43,7 @@ public class SpellbookModel extends EntityModel<SpellbookEntity> {
|
||||||
if (first_page_rot > 1) first_page_rot = 1;
|
if (first_page_rot > 1) first_page_rot = 1;
|
||||||
if (second_page_rot > 1) second_page_rot = 1;
|
if (second_page_rot > 1) second_page_rot = 1;
|
||||||
|
|
||||||
if (!entity.getIsOpen()) {
|
if (!entity.isOpen()) {
|
||||||
first_page_rot = second_page_rot = open_angle = 0;
|
first_page_rot = second_page_rot = open_angle = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,12 @@
|
||||||
package com.minelittlepony.unicopia.entity;
|
package com.minelittlepony.unicopia.entity;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import com.minelittlepony.unicopia.EquinePredicates;
|
import com.minelittlepony.unicopia.EquinePredicates;
|
||||||
import com.minelittlepony.unicopia.container.UScreenHandlers;
|
import com.minelittlepony.unicopia.container.UScreenHandlers;
|
||||||
import com.minelittlepony.unicopia.item.UItems;
|
import com.minelittlepony.unicopia.item.UItems;
|
||||||
|
|
||||||
|
import net.fabricmc.fabric.api.util.TriState;
|
||||||
import net.minecraft.entity.Entity;
|
import net.minecraft.entity.Entity;
|
||||||
import net.minecraft.entity.EntityType;
|
import net.minecraft.entity.EntityType;
|
||||||
import net.minecraft.entity.damage.DamageSource;
|
import net.minecraft.entity.damage.DamageSource;
|
||||||
|
@ -15,7 +18,6 @@ import net.minecraft.entity.player.PlayerEntity;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraft.nbt.NbtCompound;
|
import net.minecraft.nbt.NbtCompound;
|
||||||
import net.minecraft.particle.ParticleTypes;
|
import net.minecraft.particle.ParticleTypes;
|
||||||
import net.minecraft.predicate.entity.EntityPredicates;
|
|
||||||
import net.minecraft.screen.SimpleNamedScreenHandlerFactory;
|
import net.minecraft.screen.SimpleNamedScreenHandlerFactory;
|
||||||
import net.minecraft.sound.BlockSoundGroup;
|
import net.minecraft.sound.BlockSoundGroup;
|
||||||
import net.minecraft.sound.SoundCategory;
|
import net.minecraft.sound.SoundCategory;
|
||||||
|
@ -27,24 +29,27 @@ import net.minecraft.world.GameRules;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
|
|
||||||
public class SpellbookEntity extends MobEntity {
|
public class SpellbookEntity extends MobEntity {
|
||||||
private static final TrackedData<Boolean> OPENED = DataTracker.registerData(SpellbookEntity.class, TrackedDataHandlerRegistry.BOOLEAN);
|
private static final TrackedData<Boolean> AWAKE = DataTracker.registerData(SpellbookEntity.class, TrackedDataHandlerRegistry.BOOLEAN);
|
||||||
|
private static final TrackedData<Boolean> BORED = DataTracker.registerData(SpellbookEntity.class, TrackedDataHandlerRegistry.BOOLEAN);
|
||||||
|
private static final TrackedData<Byte> LOCKED = DataTracker.registerData(SpellbookEntity.class, TrackedDataHandlerRegistry.BYTE);
|
||||||
private static final TrackedData<Boolean> ALTERED = DataTracker.registerData(SpellbookEntity.class, TrackedDataHandlerRegistry.BOOLEAN);
|
private static final TrackedData<Boolean> ALTERED = DataTracker.registerData(SpellbookEntity.class, TrackedDataHandlerRegistry.BOOLEAN);
|
||||||
private static final TrackedData<Byte> OPENED_USER = DataTracker.registerData(SpellbookEntity.class, TrackedDataHandlerRegistry.BYTE);
|
|
||||||
|
private static final int TICKS_TO_SLEEP = 2000;
|
||||||
|
|
||||||
|
private int activeTicks = TICKS_TO_SLEEP;
|
||||||
|
|
||||||
public SpellbookEntity(EntityType<SpellbookEntity> type, World world) {
|
public SpellbookEntity(EntityType<SpellbookEntity> type, World world) {
|
||||||
super(type, world);
|
super(type, world);
|
||||||
setPersistent();
|
setPersistent();
|
||||||
|
setAltered(world.random.nextInt(3) == 0);
|
||||||
if (world.random.nextInt(3) == 0) {
|
|
||||||
setAltered();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void initDataTracker() {
|
protected void initDataTracker() {
|
||||||
super.initDataTracker();
|
super.initDataTracker();
|
||||||
dataTracker.startTracking(OPENED, true);
|
dataTracker.startTracking(AWAKE, true);
|
||||||
dataTracker.startTracking(OPENED_USER, (byte)1);
|
dataTracker.startTracking(BORED, false);
|
||||||
|
dataTracker.startTracking(LOCKED, (byte)1);
|
||||||
dataTracker.startTracking(ALTERED, false);
|
dataTracker.startTracking(ALTERED, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,37 +68,55 @@ public class SpellbookEntity extends MobEntity {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean getIsAltered() {
|
public boolean isAltered() {
|
||||||
return dataTracker.get(ALTERED);
|
return dataTracker.get(ALTERED);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAltered() {
|
public void setAltered(boolean altered) {
|
||||||
dataTracker.set(ALTERED, true);
|
dataTracker.set(ALTERED, altered);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean getIsOpen() {
|
protected void setLocked(TriState closed) {
|
||||||
return dataTracker.get(OPENED);
|
dataTracker.set(LOCKED, (byte)closed.ordinal());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Boolean getUserSetState() {
|
@Nullable
|
||||||
byte state = dataTracker.get(OPENED_USER);
|
protected TriState isLocked() {
|
||||||
return state == 1 ? null : state == 2;
|
return TriState.values()[Math.abs(dataTracker.get(LOCKED)) % 3];
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setIsOpen(boolean val) {
|
public boolean isOpen() {
|
||||||
dataTracker.set(OPENED, val);
|
return isLocked().orElse(isAwake()) && !isBored();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setUserSetState(Boolean val) {
|
public boolean isAwake() {
|
||||||
dataTracker.set(OPENED_USER, val == null ? (byte)1 : val == true ? (byte)2 : (byte)0);
|
return dataTracker.get(AWAKE);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAwake(boolean awake) {
|
||||||
|
if (awake != isAwake()) {
|
||||||
|
dataTracker.set(AWAKE, awake);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isBored() {
|
||||||
|
return dataTracker.get(BORED);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBored(boolean bored) {
|
||||||
|
activeTicks = TICKS_TO_SLEEP;
|
||||||
|
if (bored != isBored()) {
|
||||||
|
dataTracker.set(BORED, bored);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void tick() {
|
public void tick() {
|
||||||
boolean open = getIsOpen();
|
boolean awake = isAwake();
|
||||||
jumping = open && isTouchingWater();
|
jumping = awake && isTouchingWater();
|
||||||
super.tick();
|
super.tick();
|
||||||
if (open && world.isClient) {
|
|
||||||
|
if (world.isClient && isOpen()) {
|
||||||
for (int offX = -2; offX <= 1; ++offX) {
|
for (int offX = -2; offX <= 1; ++offX) {
|
||||||
for (int offZ = -2; offZ <= 1; ++offZ) {
|
for (int offZ = -2; offZ <= 1; ++offZ) {
|
||||||
if (offX > -1 && offX < 1 && offZ == -1) {
|
if (offX > -1 && offX < 1 && offZ == -1) {
|
||||||
|
@ -114,32 +137,36 @@ public class SpellbookEntity extends MobEntity {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (open) {
|
if (awake) {
|
||||||
world.getOtherEntities(this, getBoundingBox().expand(2), EntityPredicates.EXCEPT_SPECTATOR.and(e -> e instanceof PlayerEntity)).stream().findFirst().ifPresent(player -> {
|
world.getOtherEntities(this, getBoundingBox().expand(2), EquinePredicates.PLAYER_UNICORN.and(e -> e instanceof PlayerEntity)).stream().findFirst().ifPresent(player -> {
|
||||||
Vec3d diff = player.getPos().subtract(getPos());
|
setBored(false);
|
||||||
double yaw = Math.atan2(diff.z, diff.x) * 180D / Math.PI - 90;
|
if (isOpen()) {
|
||||||
|
Vec3d diff = player.getPos().subtract(getPos());
|
||||||
|
double yaw = Math.atan2(diff.z, diff.x) * 180D / Math.PI - 90;
|
||||||
|
|
||||||
setHeadYaw((float)yaw);
|
setHeadYaw((float)yaw);
|
||||||
setYaw((float)yaw);
|
setBodyYaw((float)yaw);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!world.isClient) {
|
||||||
|
System.out.println(activeTicks);
|
||||||
|
|
||||||
|
if (activeTicks > 0 && --activeTicks <= 0) {
|
||||||
|
setBored(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (world.random.nextInt(30) == 0) {
|
if (!world.isClient && world.random.nextInt(30) == 0) {
|
||||||
float celest = world.getSkyAngleRadians(1) * 4;
|
float celest = world.getSkyAngle(1) * 4;
|
||||||
|
|
||||||
boolean isDay = celest > 3 || celest < 1;
|
boolean daytime = celest > 3 || celest < 1;
|
||||||
|
|
||||||
Boolean userState = getUserSetState();
|
setAwake(daytime);
|
||||||
|
|
||||||
boolean canToggle = (isDay != open) && (userState == null || userState == isDay);
|
if (daytime != awake && daytime == isLocked().orElse(daytime)) {
|
||||||
|
setLocked(TriState.DEFAULT);
|
||||||
if (canToggle) {
|
|
||||||
setUserSetState(null);
|
|
||||||
setIsOpen(isDay);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (userState != null && (isDay == open) && (userState == open)) {
|
|
||||||
setUserSetState(null);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -163,15 +190,14 @@ public class SpellbookEntity extends MobEntity {
|
||||||
@Override
|
@Override
|
||||||
public ActionResult interactAt(PlayerEntity player, Vec3d vec, Hand hand) {
|
public ActionResult interactAt(PlayerEntity player, Vec3d vec, Hand hand) {
|
||||||
if (player.isSneaking()) {
|
if (player.isSneaking()) {
|
||||||
boolean open = !getIsOpen();
|
setBored(false);
|
||||||
|
setAwake(!isOpen());
|
||||||
setIsOpen(open);
|
setLocked(TriState.of(isAwake()));
|
||||||
setUserSetState(open);
|
|
||||||
|
|
||||||
return ActionResult.SUCCESS;
|
return ActionResult.SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (EquinePredicates.PLAYER_UNICORN.test(player)) {
|
if (isOpen() && EquinePredicates.PLAYER_UNICORN.test(player)) {
|
||||||
|
setBored(false);
|
||||||
player.openHandledScreen(new SimpleNamedScreenHandlerFactory((syncId, inv, ply) -> UScreenHandlers.SPELL_BOOK.create(syncId, inv), getDisplayName()));
|
player.openHandledScreen(new SimpleNamedScreenHandlerFactory((syncId, inv, ply) -> UScreenHandlers.SPELL_BOOK.create(syncId, inv), getDisplayName()));
|
||||||
player.playSound(SoundEvents.BLOCK_FURNACE_FIRE_CRACKLE, 2, 1);
|
player.playSound(SoundEvents.BLOCK_FURNACE_FIRE_CRACKLE, 2, 1);
|
||||||
return ActionResult.SUCCESS;
|
return ActionResult.SUCCESS;
|
||||||
|
@ -183,20 +209,22 @@ public class SpellbookEntity extends MobEntity {
|
||||||
@Override
|
@Override
|
||||||
public void readCustomDataFromNbt(NbtCompound compound) {
|
public void readCustomDataFromNbt(NbtCompound compound) {
|
||||||
super.readCustomDataFromNbt(compound);
|
super.readCustomDataFromNbt(compound);
|
||||||
|
setAwake(compound.getBoolean("awake"));
|
||||||
setIsOpen(compound.getBoolean("open"));
|
setBored(compound.getBoolean("bored"));
|
||||||
setUserSetState(compound.contains("force_open") ? compound.getBoolean("force_open") : null);
|
setAltered(compound.getBoolean("altered"));
|
||||||
|
setLocked(compound.contains("locked") ? TriState.of(compound.getBoolean("locked")) : TriState.DEFAULT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void writeCustomDataToNbt(NbtCompound compound) {
|
public void writeCustomDataToNbt(NbtCompound compound) {
|
||||||
super.writeCustomDataToNbt(compound);
|
super.writeCustomDataToNbt(compound);
|
||||||
compound.putBoolean("open", getIsOpen());
|
compound.putBoolean("awake", isAwake());
|
||||||
|
compound.putBoolean("bored", isBored());
|
||||||
|
compound.putBoolean("altered", isAltered());
|
||||||
|
|
||||||
Boolean state = getUserSetState();
|
TriState locked = isLocked();
|
||||||
|
if (locked != TriState.DEFAULT) {
|
||||||
if (state != null) {
|
compound.putBoolean("locked", locked.get());
|
||||||
compound.putBoolean("force_open", state);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,7 +34,7 @@ public interface UEntities {
|
||||||
.dimensions(EntityDimensions.fixed(1, 1)));
|
.dimensions(EntityDimensions.fixed(1, 1)));
|
||||||
EntityType<SpellbookEntity> SPELLBOOK = register("spellbook", FabricEntityTypeBuilder.create(SpawnGroup.MISC, SpellbookEntity::new)
|
EntityType<SpellbookEntity> SPELLBOOK = register("spellbook", FabricEntityTypeBuilder.create(SpawnGroup.MISC, SpellbookEntity::new)
|
||||||
.trackRangeBlocks(200)
|
.trackRangeBlocks(200)
|
||||||
.dimensions(EntityDimensions.fixed(1, 1)));
|
.dimensions(EntityDimensions.fixed(0.9F, 0.25F)));
|
||||||
|
|
||||||
static <T extends Entity> EntityType<T> register(String name, FabricEntityTypeBuilder<T> builder) {
|
static <T extends Entity> EntityType<T> register(String name, FabricEntityTypeBuilder<T> builder) {
|
||||||
EntityType<T> type = builder.build();
|
EntityType<T> type = builder.build();
|
||||||
|
|
Loading…
Reference in a new issue