diff --git a/build.gradle b/build.gradle index aee1739..78bab42 100644 --- a/build.gradle +++ b/build.gradle @@ -26,20 +26,34 @@ dependencies { modCompile "net.fabricmc:fabric-loader:${project.loader_version}" testImplementation 'com.google.code.findbugs:jsr305:3.0.2' modCompile "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" - modCompile "io.github.onyxstudios.Cardinal-Components-API:cardinal-components-base:2.4.0" - include "io.github.onyxstudios.Cardinal-Components-API:cardinal-components-base:2.4.0" + + modCompile "io.github.onyxstudios.Cardinal-Components-API:cardinal-components-base:2.5.1" + include "io.github.onyxstudios.Cardinal-Components-API:cardinal-components-base:2.5.1" // Adds an implementation dependency on the entity module + + /* modCompile "io.github.onyxstudios.Cardinal-Components-API:cardinal-components-entity:2.4.0" include "io.github.onyxstudios.Cardinal-Components-API:cardinal-components-entity:2.4.0" modCompile "io.github.onyxstudios.Cardinal-Components-API:cardinal-components-item:2.4.0" include "io.github.onyxstudios.Cardinal-Components-API:cardinal-components-item:2.4.0" modCompile "io.github.onyxstudios.Cardinal-Components-API:cardinal-components-block:2.4.0" include "io.github.onyxstudios.Cardinal-Components-API:cardinal-components-block:2.4.0" - modCompile "com.github.vini2003:spinnery:${project.spinnery_version}" - include "com.github.vini2003:spinnery:${project.spinnery_version}" - modImplementation "io.github.prospector:modmenu:${project.modmenu_version}" - include "io.github.prospector:modmenu:${project.modmenu_version}" - modImplementation "vazkii.patchouli:Patchouli_1.16-fabric:${project.patchouli_version}" + */ + modCompile("com.github.vini2003:spinnery:${project.spinnery_version}"){ + exclude module:"fabric-api" + } + include("com.github.vini2003:spinnery:${project.spinnery_version}"){ + exclude module:"fabric-api" + } + modImplementation("io.github.prospector:modmenu:${project.modmenu_version}"){ + exclude module:"fabric-api" + } + include("io.github.prospector:modmenu:${project.modmenu_version}"){ + exclude module:"fabric-api" + } + modImplementation("vazkii.patchouli:Patchouli:${project.patchouli_version}"){ + exclude module:"fabric-api" + } //modRuntime ("com.github.Chocohead:Data-Breaker-Lower:24be1a2") /* this will expose the dependency to anyone building against your mod. You might diff --git a/gradle.properties b/gradle.properties index b4815ec..e026708 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,16 +2,16 @@ org.gradle.jvmargs=-Xmx1G # Fabric Properties # check these on https://fabricmc.net/use -minecraft_version=1.16.1 -yarn_mappings=1.16.1+build.5 -loader_version=0.8.8+build.202 +minecraft_version=1.16.2 +yarn_mappings=1.16.2+build.21 +loader_version=0.9.2+build.206 # Mod Properties mod_version=1.0-SNAPSHOT maven_group=com.redblueflame archives_base_name=herbocraft # Dependencies # currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api -fabric_version=0.13.1+build.370-1.16 -spinnery_version=3.0.39+fabric-1.16.x -modmenu_version = 1.12.1+build.15 -patchouli_version = 1.16-37.15-FABRIC \ No newline at end of file +fabric_version=0.19.0+build.398-1.16 +spinnery_version=3.1.15+fabric-1.16.x +modmenu_version = 1.14.6+build.31 +patchouli_version = 1.16-40-FABRIC \ No newline at end of file diff --git a/src/main/java/com/redblueflame/herbocraft/HerboCraft.java b/src/main/java/com/redblueflame/herbocraft/HerboCraft.java index 27aae6e..113d10f 100644 --- a/src/main/java/com/redblueflame/herbocraft/HerboCraft.java +++ b/src/main/java/com/redblueflame/herbocraft/HerboCraft.java @@ -4,7 +4,6 @@ import com.google.gson.JsonSyntaxException; import com.redblueflame.herbocraft.blocks.*; import com.redblueflame.herbocraft.components.LevelComponent; -import com.redblueflame.herbocraft.components.TurretLevelComponent; import com.redblueflame.herbocraft.entities.BulletEntity; import com.redblueflame.herbocraft.entities.SnowTurretEntity; import com.redblueflame.herbocraft.entities.TurretBaseEntity; @@ -18,17 +17,15 @@ import io.netty.buffer.Unpooled; import nerdhub.cardinal.components.api.ComponentRegistry; import nerdhub.cardinal.components.api.ComponentType; -import nerdhub.cardinal.components.api.event.EntityComponentCallback; -import nerdhub.cardinal.components.api.event.ItemComponentCallback; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder; -import net.fabricmc.fabric.api.container.ContainerProviderRegistry; import net.fabricmc.fabric.api.network.ServerSidePacketRegistry; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.entity.FabricDefaultAttributeRegistry; import net.fabricmc.fabric.api.object.builder.v1.entity.FabricEntityTypeBuilder; import net.fabricmc.fabric.api.resource.IdentifiableResourceReloadListener; import net.fabricmc.fabric.api.resource.ResourceManagerHelper; +import net.fabricmc.fabric.api.screenhandler.v1.ScreenHandlerRegistry; import net.fabricmc.fabric.api.server.PlayerStream; import net.fabricmc.fabric.api.tag.TagRegistry; import net.minecraft.block.*; @@ -39,8 +36,6 @@ import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.*; import net.minecraft.network.PacketByteBuf; -import net.minecraft.recipe.Recipe; -import net.minecraft.recipe.RecipeType; import net.minecraft.resource.ResourceManager; import net.minecraft.resource.ResourceType; import net.minecraft.server.network.ServerPlayerEntity; @@ -137,6 +132,7 @@ public class HerboCraft implements ModInitializer { //endregion //region Components + @Deprecated public static final ComponentType LEVELLING = ComponentRegistry.INSTANCE.registerIfAbsent(new Identifier(name, "levelling"), LevelComponent.class); //endregion @@ -217,14 +213,14 @@ public void onInitialize() { UPGRADER_BLOCK_ENTITY = Registry.register(Registry.BLOCK_ENTITY_TYPE, new Identifier(name, "upgrader"), BlockEntityType.Builder.create(UpgraderBlockEntity::new, UPGRADER).build(null)); // Register containers - ContainerProviderRegistry.INSTANCE.registerFactory(STERILIZER_CONTAINER, - (syncId, id, player, buf) -> new SterilizerBlockContainer(syncId, buf.readText(), player.inventory, buf.readBlockPos(), player.world)); - ContainerProviderRegistry.INSTANCE.registerFactory(GROWTH_CONTROLLER_CONTAINER, - (syncId, id, player, buf) -> new GrowthControllerContainer(syncId, buf.readText(), player.inventory, buf.readBlockPos(), player.world)); - ContainerProviderRegistry.INSTANCE.registerFactory(REPRODUCER_CONTAINER, - (syncId, id, player, buf) -> new ReproducerBlockContainer(syncId, buf.readText(), player.inventory, buf.readBlockPos(), player.world)); - ContainerProviderRegistry.INSTANCE.registerFactory(UPGRADER_CONTAINER, - (syncId, id, player, buf) -> new UpgraderBlockContainer(syncId, buf.readText(), player.inventory, buf.readBlockPos(), player.world)); + ScreenHandlerRegistry.registerExtended(STERILIZER_CONTAINER, + (id, inv, buf) -> new SterilizerBlockContainer(id, buf.readText(), inv, buf.readBlockPos(), inv.player.world)); + ScreenHandlerRegistry.registerExtended(GROWTH_CONTROLLER_CONTAINER, + (syncId, inv, buf) -> new GrowthControllerContainer(syncId, buf.readText(), inv, buf.readBlockPos(), inv.player.world)); + ScreenHandlerRegistry.registerExtended(REPRODUCER_CONTAINER, + (syncId, inv, buf) -> new ReproducerBlockContainer(syncId, buf.readText(), inv, buf.readBlockPos(), inv.player.world)); + ScreenHandlerRegistry.registerExtended(UPGRADER_CONTAINER, + (syncId, inv, buf) -> new UpgraderBlockContainer(syncId, buf.readText(), inv, buf.readBlockPos(), inv.player.world)); // Register recipes Registry.register(Registry.RECIPE_SERIALIZER, new Identifier(name, "patchouli_book"), new PatchouliBookRecipe.Serializer()); @@ -258,12 +254,6 @@ public void onInitialize() { }); }); //endregion - // Register turret Levelling system - EntityComponentCallback.event(TurretBaseEntity.class).register(TurretBaseEntity::initComponents); - - ItemComponentCallback.event(TURRET_SEED).register((stack, components) -> components.put(LEVELLING, TurretLevelComponent.getRandomStats((short) 5))); - ItemComponentCallback.event(WITHER_SEED).register((stack, components) -> components.put(LEVELLING, TurretLevelComponent.getRandomStats((short) 5))); - ItemComponentCallback.event(SNOW_SEED).register((stack, components) -> components.put(LEVELLING, TurretLevelComponent.getRandomStats((short) 5))); // Load JSON config files ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(new IdentifiableResourceReloadListener() { @Override diff --git a/src/main/java/com/redblueflame/herbocraft/blocks/GrowthControllerBlockEntity.java b/src/main/java/com/redblueflame/herbocraft/blocks/GrowthControllerBlockEntity.java index 0c80df2..5736e34 100644 --- a/src/main/java/com/redblueflame/herbocraft/blocks/GrowthControllerBlockEntity.java +++ b/src/main/java/com/redblueflame/herbocraft/blocks/GrowthControllerBlockEntity.java @@ -78,10 +78,8 @@ public CompoundTag toTag(CompoundTag tag) { public void fromTag(BlockState state, CompoundTag tag) { super.fromTag(state, tag); inventory = InventoryUtilities.read(tag); - if (inventory == null) { - inventory = new BaseInventory(10); - } - currentItem = inventory.getStack(0).getItem(); + if (inventory != null) + currentItem = inventory.getStack(0).getItem(); } // region Quality types diff --git a/src/main/java/com/redblueflame/herbocraft/blocks/GrowthControllerContainer.java b/src/main/java/com/redblueflame/herbocraft/blocks/GrowthControllerContainer.java index 3aba167..38cab5f 100644 --- a/src/main/java/com/redblueflame/herbocraft/blocks/GrowthControllerContainer.java +++ b/src/main/java/com/redblueflame/herbocraft/blocks/GrowthControllerContainer.java @@ -7,14 +7,14 @@ import net.minecraft.text.Text; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; -import spinnery.common.container.BaseContainer; +import spinnery.common.handler.BaseScreenHandler; import spinnery.common.inventory.BaseInventory; import spinnery.widget.WInterface; import spinnery.widget.WSlot; import java.util.Collection; -public class GrowthControllerContainer extends BaseContainer { +public class GrowthControllerContainer extends BaseScreenHandler { public GrowthControllerBlockEntity entity; public static final int GROWTH_CONTROLLER_INVENTORY = 1; public Text name; @@ -30,6 +30,9 @@ public GrowthControllerContainer(int synchronizationID, Text name, PlayerInvento private void initInventory() { WInterface mainInterface = getInterface(); + if (entity.inventory == null) { + entity.inventory = new BaseInventory(10); + } getInventories().put(GROWTH_CONTROLLER_INVENTORY, entity.inventory); mainInterface.createChild(WSlot::new); Collection input = WSlot.addHeadlessArray(mainInterface, 0, GROWTH_CONTROLLER_INVENTORY, 1, 1); diff --git a/src/main/java/com/redblueflame/herbocraft/blocks/ReproducerBlockContainer.java b/src/main/java/com/redblueflame/herbocraft/blocks/ReproducerBlockContainer.java index 3ebbafe..39e8329 100644 --- a/src/main/java/com/redblueflame/herbocraft/blocks/ReproducerBlockContainer.java +++ b/src/main/java/com/redblueflame/herbocraft/blocks/ReproducerBlockContainer.java @@ -7,14 +7,14 @@ import net.minecraft.text.Text; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; -import spinnery.common.container.BaseContainer; +import spinnery.common.handler.BaseScreenHandler; import spinnery.common.inventory.BaseInventory; import spinnery.widget.WInterface; import spinnery.widget.WSlot; import java.util.Collection; -public class ReproducerBlockContainer extends BaseContainer { +public class ReproducerBlockContainer extends BaseScreenHandler { public ReproducerBlockEntity entity; public static final int GROWTH_CONTROLLER_INVENTORY = 1; public Text name; @@ -30,6 +30,9 @@ public ReproducerBlockContainer(int synchronizationID, Text name, PlayerInventor private void initInventory() { WInterface mainInterface = getInterface(); + if (entity.inventory == null) { + entity.inventory = new BaseInventory(11); + } getInventories().put(GROWTH_CONTROLLER_INVENTORY, entity.inventory); mainInterface.createChild(WSlot::new); Collection input = WSlot.addHeadlessArray(mainInterface, 0, GROWTH_CONTROLLER_INVENTORY, 2, 1); diff --git a/src/main/java/com/redblueflame/herbocraft/blocks/ReproducerBlockEntity.java b/src/main/java/com/redblueflame/herbocraft/blocks/ReproducerBlockEntity.java index b9f2a36..442854b 100644 --- a/src/main/java/com/redblueflame/herbocraft/blocks/ReproducerBlockEntity.java +++ b/src/main/java/com/redblueflame/herbocraft/blocks/ReproducerBlockEntity.java @@ -45,9 +45,9 @@ public boolean checkWork() { @Override public void finishWork() { ItemStack item = new ItemStack(currentItem1.getItem(), 1); - LevelComponent item1 = ComponentsHandler.getItemComponent(inventory.getStack(0)); - LevelComponent item2 = ComponentsHandler.getItemComponent(inventory.getStack(1)); - LevelComponent target = ComponentsHandler.getItemComponent(item); + LevelComponent item1 = ComponentsHandler.getItemComponent(inventory.getStack(0), true); + LevelComponent item2 = ComponentsHandler.getItemComponent(inventory.getStack(1), true); + LevelComponent target = ComponentsHandler.createItemComponent(item); item1.reproduceWith(item2, target, new Random()); int slot = getFirstAvailableSlot(); if (slot == -1) { @@ -92,11 +92,10 @@ public CompoundTag toTag(CompoundTag tag) { public void fromTag(BlockState state, CompoundTag tag) { super.fromTag(state, tag); inventory = InventoryUtilities.read(tag); - if (inventory == null) { - inventory = new BaseInventory(11); + if (inventory != null) { + currentItem1 = inventory.getStack(0); + currentItem2 = inventory.getStack(1); } - currentItem1 = inventory.getStack(0); - currentItem2 = inventory.getStack(1); } // region Quality types diff --git a/src/main/java/com/redblueflame/herbocraft/blocks/SnowTurretSeedBlock.java b/src/main/java/com/redblueflame/herbocraft/blocks/SnowTurretSeedBlock.java index 4da08ce..3e50f32 100644 --- a/src/main/java/com/redblueflame/herbocraft/blocks/SnowTurretSeedBlock.java +++ b/src/main/java/com/redblueflame/herbocraft/blocks/SnowTurretSeedBlock.java @@ -18,13 +18,8 @@ public SnowTurretSeedBlock(Settings settings) { public Entity getEntity(World world, LevelComponent itemComp) { SnowTurretEntity entity = new SnowTurretEntity(HerboCraft.SNOW_TURRET, world); // Set the Level Component - Optional opt_comp = HerboCraft.LEVELLING.maybeGet(entity); - if (!opt_comp.isPresent()) { - throw new RuntimeException("Seems like the entity isn't compatible with the custom tags. Please check your entry."); - } - LevelComponent entityComp = opt_comp.get(); - entityComp.copyFrom(itemComp); - entity.setAttributes(entityComp); + entity.setComponent(itemComp); + entity.setAttributes(); return entity; } } diff --git a/src/main/java/com/redblueflame/herbocraft/blocks/SterilizerBlockContainer.java b/src/main/java/com/redblueflame/herbocraft/blocks/SterilizerBlockContainer.java index b631e5c..7706532 100644 --- a/src/main/java/com/redblueflame/herbocraft/blocks/SterilizerBlockContainer.java +++ b/src/main/java/com/redblueflame/herbocraft/blocks/SterilizerBlockContainer.java @@ -6,14 +6,15 @@ import net.minecraft.text.Text; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; -import spinnery.common.container.BaseContainer; + +import spinnery.common.handler.BaseScreenHandler; import spinnery.common.inventory.BaseInventory; import spinnery.widget.WInterface; import spinnery.widget.WSlot; import java.util.Collection; -public class SterilizerBlockContainer extends BaseContainer { +public class SterilizerBlockContainer extends BaseScreenHandler { public SterilizerBlockEntity entity; public static final int STERILIZER_INVENTORY = 1; public Text name; @@ -27,7 +28,9 @@ public SterilizerBlockContainer(int synchronizationID, Text name, PlayerInventor } private void initInventory() { WInterface mainInterface = getInterface(); - + if (entity.inventory == null) { + entity.inventory = new BaseInventory(1); + } getInventories().put(STERILIZER_INVENTORY, entity.inventory); mainInterface.createChild(WSlot::new); Collection items = WSlot.addHeadlessArray(mainInterface, 0, STERILIZER_INVENTORY, 1, 1); diff --git a/src/main/java/com/redblueflame/herbocraft/blocks/SterilizerBlockEntity.java b/src/main/java/com/redblueflame/herbocraft/blocks/SterilizerBlockEntity.java index 721b246..f03c2d8 100644 --- a/src/main/java/com/redblueflame/herbocraft/blocks/SterilizerBlockEntity.java +++ b/src/main/java/com/redblueflame/herbocraft/blocks/SterilizerBlockEntity.java @@ -44,6 +44,7 @@ public boolean checkWork() { public void finishWork() { LevelComponent comp = ComponentsHandler.getItemComponent(inventory.getStack(0)); comp.setSterile(true); + ComponentsHandler.saveItemComponent(inventory.getStack(0), comp); } @Override @@ -83,9 +84,7 @@ public CompoundTag toTag(CompoundTag tag) { public void fromTag(BlockState state, CompoundTag tag) { super.fromTag(state, tag); inventory = InventoryUtilities.read(tag); - if (inventory == null) { - inventory = new BaseInventory(1); - } - currentItem = inventory.getStack(0); + if (inventory != null) + currentItem = inventory.getStack(0); } } diff --git a/src/main/java/com/redblueflame/herbocraft/blocks/TurretSeedBlock.java b/src/main/java/com/redblueflame/herbocraft/blocks/TurretSeedBlock.java index a38840e..0bf9518 100644 --- a/src/main/java/com/redblueflame/herbocraft/blocks/TurretSeedBlock.java +++ b/src/main/java/com/redblueflame/herbocraft/blocks/TurretSeedBlock.java @@ -39,14 +39,9 @@ public TurretSeedBlock(Settings settings) { public Entity getEntity(World world, LevelComponent itemComp) { TurretBaseEntity entity = new TurretBaseEntity(HerboCraft.TURRET_BASE, world); - // Set the Level Component - Optional opt_comp = HerboCraft.LEVELLING.maybeGet(entity); - if (!opt_comp.isPresent()) { - throw new RuntimeException("Seems like the entity isn't compatible with the custom tags. Please check your entry."); - } - LevelComponent entityComp = opt_comp.get(); - entityComp.copyFrom(itemComp); - entity.setAttributes(entityComp); + + entity.setComponent(itemComp); + entity.setAttributes(); return entity; } diff --git a/src/main/java/com/redblueflame/herbocraft/blocks/TurretSeedBlockEntity.java b/src/main/java/com/redblueflame/herbocraft/blocks/TurretSeedBlockEntity.java index ff937de..f452bfb 100644 --- a/src/main/java/com/redblueflame/herbocraft/blocks/TurretSeedBlockEntity.java +++ b/src/main/java/com/redblueflame/herbocraft/blocks/TurretSeedBlockEntity.java @@ -13,7 +13,7 @@ public class TurretSeedBlockEntity extends BlockEntity { private LevelComponent component; public TurretSeedBlockEntity() { super(HerboCraft.TURRET_SEED_BLOCK_ENTITY); - component = new TurretLevelComponent(); + component = null; } public LevelComponent getComponent() { return component; @@ -30,7 +30,9 @@ public void fromTag(BlockState state, CompoundTag tag) { @Override public CompoundTag toTag(CompoundTag tag) { super.toTag(tag); - component.toTag(tag); + if (component != null) { + component.toTag(tag); + } return tag; } } diff --git a/src/main/java/com/redblueflame/herbocraft/blocks/UpgraderBlockContainer.java b/src/main/java/com/redblueflame/herbocraft/blocks/UpgraderBlockContainer.java index c304e79..b84a51b 100644 --- a/src/main/java/com/redblueflame/herbocraft/blocks/UpgraderBlockContainer.java +++ b/src/main/java/com/redblueflame/herbocraft/blocks/UpgraderBlockContainer.java @@ -7,14 +7,14 @@ import net.minecraft.text.Text; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; -import spinnery.common.container.BaseContainer; +import spinnery.common.handler.BaseScreenHandler; import spinnery.common.inventory.BaseInventory; import spinnery.widget.WInterface; import spinnery.widget.WSlot; import java.util.Collection; -public class UpgraderBlockContainer extends BaseContainer { +public class UpgraderBlockContainer extends BaseScreenHandler { public UpgraderBlockEntity entity; public static final int GROWTH_CONTROLLER_INVENTORY = 1; public Text name; @@ -30,7 +30,9 @@ public UpgraderBlockContainer(int synchronizationID, Text name, PlayerInventory private void initInventory() { WInterface mainInterface = getInterface(); - + if (entity.inventory == null) { + entity.inventory = new BaseInventory(20); + } getInventories().put(GROWTH_CONTROLLER_INVENTORY, entity.inventory); mainInterface.createChild(WSlot::new); Collection input = WSlot.addHeadlessArray(mainInterface, 0, GROWTH_CONTROLLER_INVENTORY, 3, 3); diff --git a/src/main/java/com/redblueflame/herbocraft/blocks/UpgraderBlockEntity.java b/src/main/java/com/redblueflame/herbocraft/blocks/UpgraderBlockEntity.java index 95bad1c..75cc319 100644 --- a/src/main/java/com/redblueflame/herbocraft/blocks/UpgraderBlockEntity.java +++ b/src/main/java/com/redblueflame/herbocraft/blocks/UpgraderBlockEntity.java @@ -54,17 +54,19 @@ public void finishWork() { return; } ItemStack item = inventory.getStack(10); - Optional comp = ComponentsHandler.getOptionalItemComponent(item); - if (!comp.isPresent()) { + Optional comp_opt = ComponentsHandler.getOptionalItemComponent(item); + if (!comp_opt.isPresent()) { // The component is not present, we do nothing. resetWork(); return; } - comp.get().addLevels(1); - comp.get().setStability(comp.get().getStability()-25); + LevelComponent comp = comp_opt.get(); + comp.addLevels(1); + comp.setStability(comp.getStability()-25); + ComponentsHandler.saveItemComponent(item, comp); // Move out the item if it's fullt upgraded - if (comp.get().getStability() <= 25) { + if (comp_opt.get().getStability() <= 25) { // Move the item to an output spot int target = getFirstAvailableSlot(); if (target == -1) { @@ -106,10 +108,8 @@ public CompoundTag toTag(CompoundTag tag) { public void fromTag(BlockState state, CompoundTag tag) { super.fromTag(state, tag); inventory = InventoryUtilities.read(tag); - if (inventory == null) { - inventory = new BaseInventory(20); - } - currentItem = inventory.getStack(10); + if (inventory != null) + currentItem = inventory.getStack(10); } private boolean isAccepted(ItemStack item) { diff --git a/src/main/java/com/redblueflame/herbocraft/blocks/WitherTurretSeedBlock.java b/src/main/java/com/redblueflame/herbocraft/blocks/WitherTurretSeedBlock.java index 230cab0..3ca0d12 100644 --- a/src/main/java/com/redblueflame/herbocraft/blocks/WitherTurretSeedBlock.java +++ b/src/main/java/com/redblueflame/herbocraft/blocks/WitherTurretSeedBlock.java @@ -16,14 +16,8 @@ public WitherTurretSeedBlock(Settings settings) { @Override public Entity getEntity(World world, LevelComponent itemComp) { WitherTurretEntity entity = new WitherTurretEntity(HerboCraft.WITHER_TURRET, world); - // Set the Level Component - Optional opt_comp = HerboCraft.LEVELLING.maybeGet(entity); - if (!opt_comp.isPresent()) { - throw new RuntimeException("Seems like the entity isn't compatible with the custom tags. Please check your entry."); - } - LevelComponent entityComp = opt_comp.get(); - entityComp.copyFrom(itemComp); - entity.setAttributes(entityComp); + entity.setComponent(itemComp); + entity.setAttributes(); return entity; } } diff --git a/src/main/java/com/redblueflame/herbocraft/components/LevelComponent.java b/src/main/java/com/redblueflame/herbocraft/components/LevelComponent.java index 4682fa8..10e233c 100644 --- a/src/main/java/com/redblueflame/herbocraft/components/LevelComponent.java +++ b/src/main/java/com/redblueflame/herbocraft/components/LevelComponent.java @@ -6,7 +6,7 @@ import java.util.Random; -public interface LevelComponent extends Component, CopyableComponent { +public interface LevelComponent extends Component { int getHealth(); void setHealth(int health); float getAttackSpeed(); diff --git a/src/main/java/com/redblueflame/herbocraft/components/TurretLevelComponent.java b/src/main/java/com/redblueflame/herbocraft/components/TurretLevelComponent.java index f8a497d..3a0f447 100644 --- a/src/main/java/com/redblueflame/herbocraft/components/TurretLevelComponent.java +++ b/src/main/java/com/redblueflame/herbocraft/components/TurretLevelComponent.java @@ -7,12 +7,13 @@ import java.util.Random; -public class TurretLevelComponent implements LevelComponent, CopyableComponent { +public class TurretLevelComponent implements LevelComponent { private int health = 5; private float attackSpeed = 1; private int damage = 2; private int durability = 200; private int stability = 255; + private boolean dirty = true; private boolean sterile = false; public static TurretLevelComponent getRandomStats(short level) { @@ -63,18 +64,19 @@ private void addRandomStats(int level) { private void addLevelToStat(String name) { switch (name) { case "Health": - health += 4; + health += 1; break; case "AttackSpeed": - attackSpeed += 0.2; + attackSpeed += 0.1; break; case "Damage": - damage += 1; + damage += 0.5; break; case "Durability": durability += 1; break; } + dirty = true; } @Override @@ -87,6 +89,7 @@ public void reproduceWith(LevelComponent other, LevelComponent target, Random rd target.setDamage(Math.min(this.damage, other.getDamage()) + rdm.nextInt(sep+1)); target.setDurability(Math.min(this.durability, other.getDurability()) + rdm.nextInt(50)); target.setStability(200 + rdm.nextInt(55)); + dirty = true; } @Override @@ -97,6 +100,7 @@ public int getHealth() { @Override public void setHealth(int health) { this.health = health; + dirty = true; } @Override @@ -107,6 +111,7 @@ public float getAttackSpeed() { @Override public void setAttackSpeed(float attackSpeed) { this.attackSpeed = attackSpeed; + dirty = true; } @Override @@ -117,6 +122,7 @@ public int getDamage() { @Override public void setDamage(int damage) { this.damage = damage; + dirty = true; } @Override @@ -127,6 +133,7 @@ public int getDurability() { @Override public void setDurability(int durability) { this.durability = durability; + dirty = true; } @Override @@ -142,11 +149,13 @@ public boolean isSterile() { @Override public void setSterile(boolean sterile) { this.sterile = sterile; + dirty = true; } @Override public void setStability(int stability) { this.stability = stability; + dirty = true; } @Override @@ -170,12 +179,6 @@ public CompoundTag toTag(CompoundTag tag) { return tag; } - @Override - public ComponentType getComponentType() { - return HerboCraft.LEVELLING; - } - - @Override public void copyFrom(LevelComponent other) { this.health = other.getHealth(); this.durability = other.getDurability(); @@ -184,4 +187,24 @@ public void copyFrom(LevelComponent other) { this.sterile = other.isSterile(); this.stability = other.getStability(); } + + @Override + public String toString() { + return "TurretLevelComponent{" + + "health=" + health + + ", attackSpeed=" + attackSpeed + + ", damage=" + damage + + ", durability=" + durability + + ", stability=" + stability + + ", dirty=" + dirty + + ", sterile=" + sterile + + '}'; + } + + public boolean isDirty() { + return dirty; + } + public void clearDirty() { + dirty = false; + } } \ No newline at end of file diff --git a/src/main/java/com/redblueflame/herbocraft/entities/SnowTurretEntity.java b/src/main/java/com/redblueflame/herbocraft/entities/SnowTurretEntity.java index 0216c19..2ca8c6c 100644 --- a/src/main/java/com/redblueflame/herbocraft/entities/SnowTurretEntity.java +++ b/src/main/java/com/redblueflame/herbocraft/entities/SnowTurretEntity.java @@ -2,11 +2,11 @@ import net.minecraft.entity.EntityType; import net.minecraft.entity.effect.StatusEffects; -import net.minecraft.entity.mob.MobEntityWithAi; +import net.minecraft.entity.mob.PathAwareEntity; import net.minecraft.world.World; public class SnowTurretEntity extends TurretBaseEntity { - public SnowTurretEntity(EntityType entityType, World world) { + public SnowTurretEntity(EntityType entityType, World world) { super(entityType, world); } diff --git a/src/main/java/com/redblueflame/herbocraft/entities/TurretBaseEntity.java b/src/main/java/com/redblueflame/herbocraft/entities/TurretBaseEntity.java index 6d583b5..dad0c1d 100644 --- a/src/main/java/com/redblueflame/herbocraft/entities/TurretBaseEntity.java +++ b/src/main/java/com/redblueflame/herbocraft/entities/TurretBaseEntity.java @@ -23,8 +23,8 @@ import net.minecraft.entity.data.TrackedDataHandlerRegistry; import net.minecraft.entity.effect.StatusEffects; import net.minecraft.entity.mob.MobEntity; -import net.minecraft.entity.mob.MobEntityWithAi; import net.minecraft.entity.mob.Monster; +import net.minecraft.entity.mob.PathAwareEntity; import net.minecraft.entity.passive.SnowGolemEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.nbt.CompoundTag; @@ -32,18 +32,20 @@ import net.minecraft.util.math.MathHelper; import net.minecraft.world.World; -public class TurretBaseEntity extends MobEntityWithAi implements RangedAttackMob { +public class TurretBaseEntity extends PathAwareEntity implements RangedAttackMob { private static final TrackedData TURRET_BASE_FLAGS; private Goal projectileGoal; + private LevelComponent component; public float damage = 40F; static { TURRET_BASE_FLAGS = DataTracker.registerData(TurretBaseEntity.class, TrackedDataHandlerRegistry.BYTE); } - public TurretBaseEntity(EntityType entityType, World world) { + public TurretBaseEntity(EntityType entityType, World world) { super(entityType, world); projectileGoal = new ProjectileAttackGoal(this, 0D, 10, 40F); + component = new TurretLevelComponent(); } public static DefaultAttributeContainer.Builder createBaseTurretAttributes() { @@ -53,7 +55,7 @@ public static DefaultAttributeContainer.Builder createBaseTurretAttributes() { .add(EntityAttributes.GENERIC_ATTACK_SPEED, 2D); } - public void setAttributes(LevelComponent component) { + public void setAttributes() { this.getAttributeInstance(EntityAttributes.GENERIC_MAX_HEALTH).setBaseValue(component.getHealth()); this.getAttributeInstance(EntityAttributes.GENERIC_ATTACK_SPEED).setBaseValue(component.getAttackSpeed()); this.goalSelector.remove(projectileGoal); @@ -79,9 +81,6 @@ protected void initDataTracker() { super.initDataTracker(); this.dataTracker.startTracking(TURRET_BASE_FLAGS, (byte) 16); } - public void initComponents(ComponentContainer components) { - components.put(HerboCraft.LEVELLING, new EntityTurretComponent(this, (short) 5)); - } @Override public void attack(LivingEntity target, float pullProgress) { BulletEntity bulletEntity = getBulletEntity(); @@ -151,6 +150,25 @@ public boolean canImmediatelyDespawn(double distanceSquared) { @Override public void readCustomDataFromTag(CompoundTag tag) { super.readCustomDataFromTag(tag); - setAttributes(HerboCraft.LEVELLING.get(this)); + // Extract the component + LevelComponent comp = new TurretLevelComponent(); + comp.fromTag(tag.getCompound("LevelComponent")); + component = comp; + setAttributes(); + } + + @Override + public void writeCustomDataToTag(CompoundTag tag) { + super.writeCustomDataToTag(tag); + CompoundTag subTag = tag.getCompound("LevelComponent"); + tag.put("LevelComponent", component.toTag(subTag)); + } + + public LevelComponent getComponent() { + return component; + } + + public void setComponent(LevelComponent component) { + this.component = component; } } diff --git a/src/main/java/com/redblueflame/herbocraft/entities/WitherTurretEntity.java b/src/main/java/com/redblueflame/herbocraft/entities/WitherTurretEntity.java index c2e4fa8..cb29734 100644 --- a/src/main/java/com/redblueflame/herbocraft/entities/WitherTurretEntity.java +++ b/src/main/java/com/redblueflame/herbocraft/entities/WitherTurretEntity.java @@ -2,11 +2,11 @@ import net.minecraft.entity.EntityType; import net.minecraft.entity.effect.StatusEffects; -import net.minecraft.entity.mob.MobEntityWithAi; +import net.minecraft.entity.mob.PathAwareEntity; import net.minecraft.world.World; public class WitherTurretEntity extends TurretBaseEntity { - public WitherTurretEntity(EntityType entityType, World world) { + public WitherTurretEntity(EntityType entityType, World world) { super(entityType, world); } diff --git a/src/main/java/com/redblueflame/herbocraft/items/TurretAnalyzer.java b/src/main/java/com/redblueflame/herbocraft/items/TurretAnalyzer.java index 2180d76..9b11c9a 100644 --- a/src/main/java/com/redblueflame/herbocraft/items/TurretAnalyzer.java +++ b/src/main/java/com/redblueflame/herbocraft/items/TurretAnalyzer.java @@ -15,6 +15,7 @@ import net.minecraft.util.Hand; import net.minecraft.world.World; +import java.io.Console; import java.util.Optional; import java.util.logging.Level; @@ -26,17 +27,20 @@ public TurretAnalyzer(Settings settings) { @Override public ActionResult useOnEntity(ItemStack stack, PlayerEntity user, LivingEntity entity, Hand hand) { - Optional opt_comp = HerboCraft.LEVELLING.maybeGet(entity); - if (user.isSneaking() && opt_comp.isPresent() && currentId != entity.getEntityId()) { + if (!(entity instanceof TurretBaseEntity)) { + return ActionResult.FAIL; + } + TurretBaseEntity turretEntity = (TurretBaseEntity) entity; + if (user.isSneaking() && turretEntity.getComponent() != null && currentId != entity.getEntityId()) { currentId = entity.getEntityId(); - LevelComponent comp = opt_comp.get(); + LevelComponent comp = turretEntity.getComponent(); user.sendMessage(new TranslatableText("level_tooltips.health", comp.getHealth()).setStyle(Style.EMPTY.withColor(Formatting.GRAY)), false); user.sendMessage(new TranslatableText("level_tooltips.attack_speed", comp.getAttackSpeed()).setStyle(Style.EMPTY.withColor(Formatting.GRAY)), false); user.sendMessage(new TranslatableText("level_tooltips.damage", comp.getDamage()).setStyle(Style.EMPTY.withColor(Formatting.GRAY)), false); user.sendMessage(new TranslatableText("level_tooltips.durability", Converters.getKeyFromQuality(Converters.getQualityFromDurability(comp.getDurability()))).setStyle(Style.EMPTY.withColor(Formatting.GRAY)), false); user.sendMessage(new TranslatableText("level_tooltips.stability", Converters.getKeyFromQuality(Converters.getQualityFromStability(comp.getStability()))).setStyle(Style.EMPTY.withColor(Formatting.GRAY)), false); - } else if (!opt_comp.isPresent() && entity instanceof TurretBaseEntity) { - System.out.println("There were an error, the component is not present !"); + } else if (turretEntity.getComponent() == null) { + HerboCraft.LOGGER.warn("The turret component is not initialized !"); } return ActionResult.PASS; } diff --git a/src/main/java/com/redblueflame/herbocraft/items/TurretSeed.java b/src/main/java/com/redblueflame/herbocraft/items/TurretSeed.java index ab2b3f9..c431533 100644 --- a/src/main/java/com/redblueflame/herbocraft/items/TurretSeed.java +++ b/src/main/java/com/redblueflame/herbocraft/items/TurretSeed.java @@ -4,6 +4,8 @@ import com.redblueflame.herbocraft.blocks.TurretSeedBlockEntity; import com.redblueflame.herbocraft.components.Converters; import com.redblueflame.herbocraft.components.LevelComponent; +import com.redblueflame.herbocraft.components.TurretLevelComponent; +import com.redblueflame.herbocraft.utils.ComponentsHandler; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.block.Block; @@ -42,10 +44,25 @@ public boolean isIn(Tag tag) { return super.isIn(tag); } + + @Override + public boolean postProcessTag(CompoundTag tag) { + CompoundTag subtag = new CompoundTag(); + subtag = ComponentsHandler.createComponentTag(subtag); + tag.put("LevelComponent", subtag); + return true; + } + @Environment(EnvType.CLIENT) @Override public void appendTooltip(ItemStack stack, World world, List lines, TooltipContext ctx) { - LevelComponent comp = HerboCraft.LEVELLING.get(stack); + LevelComponent comp = ComponentsHandler.getItemComponent(stack); + + if (comp == null) { + // HerboCraft.LOGGER.error("The seed does not have the component attached to it !"); + lines.add(new TranslatableText("level_tooltips.unknown").setStyle(Style.EMPTY.withColor(Formatting.YELLOW))); + return; + } lines.add(new TranslatableText("level_tooltips.health", comp.getHealth()).setStyle(Style.EMPTY.withColor(Formatting.GRAY))); DecimalFormat numberFormat = new DecimalFormat("#.00"); @@ -60,7 +77,8 @@ public void appendTooltip(ItemStack stack, World world, List lines, Toolti @Override protected boolean postPlacement(BlockPos pos, World world, PlayerEntity player, ItemStack stack, BlockState state) { - LevelComponent comp = HerboCraft.LEVELLING.get(stack); + System.out.println("Adding component!"); + LevelComponent comp = ComponentsHandler.getItemComponent(stack); return writeTagToBlockEntity(world, player, pos, stack, comp); } @@ -73,11 +91,6 @@ public static boolean writeTagToBlockEntity(World world, PlayerEntity player, Bl return false; } - CompoundTag compoundTag2 = blockEntity.toTag(new CompoundTag()); - compoundTag2.putInt("x", pos.getX()); - compoundTag2.putInt("y", pos.getY()); - compoundTag2.putInt("z", pos.getZ()); - blockEntity.fromTag(world.getBlockState(pos), compoundTag2); // Also put the components here if (!(blockEntity instanceof TurretSeedBlockEntity)) { System.out.println("There is a problem, the block entity is not the expected one..."); @@ -85,10 +98,17 @@ public static boolean writeTagToBlockEntity(World world, PlayerEntity player, Bl return true; } ((TurretSeedBlockEntity) blockEntity).setComponent(component); + + CompoundTag compoundTag2 = blockEntity.toTag(new CompoundTag()); + compoundTag2.putInt("x", pos.getX()); + compoundTag2.putInt("y", pos.getY()); + compoundTag2.putInt("z", pos.getZ()); + blockEntity.fromTag(world.getBlockState(pos), compoundTag2); blockEntity.markDirty(); return true; } } return false; } + } \ No newline at end of file diff --git a/src/main/java/com/redblueflame/herbocraft/mixins/FixTagInit.java b/src/main/java/com/redblueflame/herbocraft/mixins/FixTagInit.java new file mode 100644 index 0000000..f31adf5 --- /dev/null +++ b/src/main/java/com/redblueflame/herbocraft/mixins/FixTagInit.java @@ -0,0 +1,30 @@ +package com.redblueflame.herbocraft.mixins; + +import com.redblueflame.herbocraft.items.TurretSeed; +import net.minecraft.item.ItemConvertible; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.CompoundTag; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import javax.xml.crypto.dsig.CanonicalizationMethod; + +@Mixin(ItemStack.class) +public class FixTagInit { + @Shadow + private CompoundTag tag; + + @Inject(method = "(Lnet/minecraft/item/ItemConvertible;I)V", at=@At(value = "TAIL", target = "Lnet/minecraft/item/ItemStack;updateEmptyState()V")) + public void addTags(ItemConvertible item, int count, CallbackInfo ci) { + if (item instanceof TurretSeed) { + // Create the tag + if (tag == null) { + tag = new CompoundTag(); + ((TurretSeed) item).postProcessTag(tag); + } + } + } +} diff --git a/src/main/java/com/redblueflame/herbocraft/ui/GrowthControllerInterface.java b/src/main/java/com/redblueflame/herbocraft/ui/GrowthControllerInterface.java index 87ba866..8008ace 100644 --- a/src/main/java/com/redblueflame/herbocraft/ui/GrowthControllerInterface.java +++ b/src/main/java/com/redblueflame/herbocraft/ui/GrowthControllerInterface.java @@ -7,14 +7,14 @@ import net.minecraft.entity.player.PlayerEntity; import net.minecraft.text.Text; import net.minecraft.util.Identifier; -import spinnery.client.screen.BaseContainerScreen; +import spinnery.client.screen.BaseHandledScreen; import spinnery.widget.*; import spinnery.widget.api.Position; import spinnery.widget.api.Size; import java.util.Collection; -public class GrowthControllerInterface extends BaseContainerScreen { +public class GrowthControllerInterface extends BaseHandledScreen { private WHorizontalLoadingBar img; public GrowthControllerInterface(GrowthControllerContainer linkedContainer) { super(linkedContainer.name, linkedContainer, linkedContainer.player); @@ -45,7 +45,7 @@ private void initializeInterface(GrowthControllerContainer container) { @Override public void render(MatrixStack matrices, int mouseX, int mouseY, float tickDelta) { - img.setState((short) getContainer().entity.state); + img.setState((short) getScreenHandler().entity.state); super.render(matrices, mouseX, mouseY, tickDelta); } diff --git a/src/main/java/com/redblueflame/herbocraft/ui/ReproducerBlockInterface.java b/src/main/java/com/redblueflame/herbocraft/ui/ReproducerBlockInterface.java index 712b428..6a20ec0 100644 --- a/src/main/java/com/redblueflame/herbocraft/ui/ReproducerBlockInterface.java +++ b/src/main/java/com/redblueflame/herbocraft/ui/ReproducerBlockInterface.java @@ -6,12 +6,12 @@ import com.redblueflame.herbocraft.blocks.SterilizerBlockContainer; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.util.Identifier; -import spinnery.client.screen.BaseContainerScreen; +import spinnery.client.screen.BaseHandledScreen; import spinnery.widget.*; import spinnery.widget.api.Position; import spinnery.widget.api.Size; -public class ReproducerBlockInterface extends BaseContainerScreen { +public class ReproducerBlockInterface extends BaseHandledScreen { private WHorizontalLoadingBar img; public ReproducerBlockInterface(ReproducerBlockContainer linkedContainer) { super(linkedContainer.name, linkedContainer, linkedContainer.player); @@ -42,7 +42,7 @@ private void initializeInterface(ReproducerBlockContainer container) { @Override public void render(MatrixStack matrices, int mouseX, int mouseY, float tickDelta) { - img.setState((short) getContainer().entity.state); + img.setState((short) getScreenHandler().entity.state); super.render(matrices, mouseX, mouseY, tickDelta); } diff --git a/src/main/java/com/redblueflame/herbocraft/ui/SterilizerInterface.java b/src/main/java/com/redblueflame/herbocraft/ui/SterilizerInterface.java index 0f370fe..af458c6 100644 --- a/src/main/java/com/redblueflame/herbocraft/ui/SterilizerInterface.java +++ b/src/main/java/com/redblueflame/herbocraft/ui/SterilizerInterface.java @@ -9,14 +9,14 @@ import net.minecraft.text.Text; import net.minecraft.util.Identifier; import org.apache.http.client.utils.Idn; -import spinnery.client.screen.BaseContainerScreen; +import spinnery.client.screen.BaseHandledScreen; import spinnery.widget.*; import spinnery.widget.api.Position; import spinnery.widget.api.Size; import java.util.Collection; -public class SterilizerInterface extends BaseContainerScreen { +public class SterilizerInterface extends BaseHandledScreen { private WVerticalLoadingBar img; public SterilizerInterface(SterilizerBlockContainer container) { super(container.name, container, container.player); @@ -43,7 +43,7 @@ private void initInterface(SterilizerBlockContainer container) { @Override public void render(MatrixStack matrices, int mouseX, int mouseY, float tickDelta) { - img.setState((short) getContainer().entity.state); + img.setState((short) getScreenHandler().entity.state); super.render(matrices, mouseX, mouseY, tickDelta); } } diff --git a/src/main/java/com/redblueflame/herbocraft/ui/UpgraderBlockInterface.java b/src/main/java/com/redblueflame/herbocraft/ui/UpgraderBlockInterface.java index 45d32e1..86e0c99 100644 --- a/src/main/java/com/redblueflame/herbocraft/ui/UpgraderBlockInterface.java +++ b/src/main/java/com/redblueflame/herbocraft/ui/UpgraderBlockInterface.java @@ -5,12 +5,12 @@ import com.redblueflame.herbocraft.blocks.UpgraderBlockContainer; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.util.Identifier; -import spinnery.client.screen.BaseContainerScreen; +import spinnery.client.screen.BaseHandledScreen; import spinnery.widget.*; import spinnery.widget.api.Position; import spinnery.widget.api.Size; -public class UpgraderBlockInterface extends BaseContainerScreen { +public class UpgraderBlockInterface extends BaseHandledScreen { private WVerticalLoadingBar spinner; private short spinner_state; private WVerticalLoadingBar progress; @@ -70,14 +70,14 @@ private void initializeInterface(UpgraderBlockContainer container) { @Override public void render(MatrixStack matrices, int mouseX, int mouseY, float tickDelta) { // Add spinner state - if (getContainer().entity.isWorking) { + if (getScreenHandler().entity.isWorking) { spinner_state += 2; if (spinner_state > 255) { spinner_state = 0; } } this.spinner.setState(spinner_state); - this.progress.setState((short) getContainer().entity.state); + this.progress.setState((short) getScreenHandler().entity.state); super.render(matrices, mouseX, mouseY, tickDelta); } } diff --git a/src/main/java/com/redblueflame/herbocraft/ui/WHorizontalLoadingBar.java b/src/main/java/com/redblueflame/herbocraft/ui/WHorizontalLoadingBar.java index 5cbbb24..1d65c7b 100644 --- a/src/main/java/com/redblueflame/herbocraft/ui/WHorizontalLoadingBar.java +++ b/src/main/java/com/redblueflame/herbocraft/ui/WHorizontalLoadingBar.java @@ -16,7 +16,7 @@ public short getState() { } @Override - public void draw(MatrixStack matrices, VertexConsumerProvider.Immediate provider) { + public void draw(MatrixStack matrices, VertexConsumerProvider provider) { if (!this.isHidden()) { float x = this.getX(); float y = this.getY(); diff --git a/src/main/java/com/redblueflame/herbocraft/ui/WVerticalLoadingBar.java b/src/main/java/com/redblueflame/herbocraft/ui/WVerticalLoadingBar.java index b1b5666..0998214 100644 --- a/src/main/java/com/redblueflame/herbocraft/ui/WVerticalLoadingBar.java +++ b/src/main/java/com/redblueflame/herbocraft/ui/WVerticalLoadingBar.java @@ -18,7 +18,7 @@ public short getState() { } @Override - public void draw(MatrixStack matrices, VertexConsumerProvider.Immediate provider) { + public void draw(MatrixStack matrices, VertexConsumerProvider provider) { if (!this.isHidden()) { float height = this.getHeight() * (state / 255F); float x = this.getX(); diff --git a/src/main/java/com/redblueflame/herbocraft/utils/ComponentsHandler.java b/src/main/java/com/redblueflame/herbocraft/utils/ComponentsHandler.java index 35fa4a6..911e9f4 100644 --- a/src/main/java/com/redblueflame/herbocraft/utils/ComponentsHandler.java +++ b/src/main/java/com/redblueflame/herbocraft/utils/ComponentsHandler.java @@ -1,18 +1,102 @@ package com.redblueflame.herbocraft.utils; import com.redblueflame.herbocraft.HerboCraft; +import com.redblueflame.herbocraft.blocks.TurretSeedBlockEntity; import com.redblueflame.herbocraft.components.LevelComponent; import com.redblueflame.herbocraft.components.TurretLevelComponent; +import com.redblueflame.herbocraft.items.TurretSeed; +import net.minecraft.block.entity.BlockEntity; import net.minecraft.item.ItemStack; -import net.minecraft.util.ItemScatterer; +import net.minecraft.nbt.CompoundTag; import java.util.Optional; public class ComponentsHandler { + + public static LevelComponent getBlockEntityComponent(BlockEntity entity) { + return getBlockEntityComponent(entity, true); + } + + public static LevelComponent getBlockEntityComponent(BlockEntity entity, boolean create) { + if (!(entity instanceof TurretSeedBlockEntity)) { + // The blockentity is not supported + HerboCraft.LOGGER.error("The blockentity is not supported."); + return null; + } + LevelComponent comp = ((TurretSeedBlockEntity) entity).getComponent(); + if (comp == null && create) { + comp = TurretLevelComponent.getRandomStats((short) 5); + ((TurretSeedBlockEntity) entity).setComponent(comp); + } + return comp; + } + + public static Optional getOptionalBlockEntityComponent(BlockEntity entity) { + LevelComponent comp = getBlockEntityComponent(entity, false); + if (comp == null) { + return Optional.empty(); + } + return Optional.of(comp); + } + + + public static void setBlockEntityComponent(BlockEntity entity, LevelComponent component) { + if (!(entity instanceof TurretSeedBlockEntity)) { + // The blockentity is not supported + HerboCraft.LOGGER.error("The blockentity is not supported."); + return; + } + ((TurretSeedBlockEntity) entity).setComponent(component); + } + public static LevelComponent getItemComponent(ItemStack stack) { - return HerboCraft.LEVELLING.get(stack); + return getItemComponent(stack, false); + } + public static LevelComponent getItemComponent(ItemStack stack, boolean create) { + if (!(stack.getItem() instanceof TurretSeed)) { + return null; + } + CompoundTag levelTag; + if (create) { + levelTag = stack.getSubTag("LevelComponent"); + if (levelTag == null) { + levelTag = new CompoundTag(); + levelTag = createComponentTag(levelTag); + stack.putSubTag("LevelComponent", levelTag); + } + } else { + levelTag = stack.getSubTag("LevelComponent"); + } + if (levelTag == null) { + return null; + } + TurretLevelComponent component = new TurretLevelComponent(); + component.fromTag(levelTag); + return component; + } + + public static LevelComponent createItemComponent(ItemStack stack) { + TurretLevelComponent comp = TurretLevelComponent.getRandomStats((short) 5); + CompoundTag tag = stack.getOrCreateSubTag("LevelComponent"); + tag = comp.toTag(tag); + stack.setTag(tag); + return comp; } + public static CompoundTag createComponentTag(CompoundTag tag) { + TurretLevelComponent comp = TurretLevelComponent.getRandomStats((short) 5); + return comp.toTag(tag); + } + + public static void saveItemComponent(ItemStack stack, LevelComponent component) { + CompoundTag levelTag = new CompoundTag(); + stack.putSubTag("LevelComponent", component.toTag(levelTag)); + } + public static Optional getOptionalItemComponent(ItemStack stack) { - return HerboCraft.LEVELLING.maybeGet(stack); + LevelComponent component = getItemComponent(stack); + if (component == null) { + return Optional.empty(); + } + return Optional.of(component); } } diff --git a/src/main/java/com/redblueflame/herbocraft/utils/TurretLooter.java b/src/main/java/com/redblueflame/herbocraft/utils/TurretLooter.java index b8907fd..4d815d4 100644 --- a/src/main/java/com/redblueflame/herbocraft/utils/TurretLooter.java +++ b/src/main/java/com/redblueflame/herbocraft/utils/TurretLooter.java @@ -46,17 +46,13 @@ public ItemStack getRandomTurret(QualityType quality) { TurretDrop pickedDrop = drawRandomTurret(quality); Item pickedItem = Registry.ITEM.get(Identifier.tryParse(pickedDrop.id)); ItemStack itemStack = new ItemStack(pickedItem, 1); - // Add cardinal components to it - Optional opt_comp = HerboCraft.LEVELLING.maybeGet(itemStack); - if (!opt_comp.isPresent()) { - // This is not a plant that's compatible... - throw new RuntimeException("This plant is not compatible with the LevelComponent. Please fix that"); - } - LevelComponent comp = opt_comp.get(); + // As this is a new item, we can directly create a new component + LevelComponent comp = ComponentsHandler.createItemComponent(itemStack); // Get the levels needed comp.resetLevels(); comp.addLevels(pickedDrop.level.getRandom(random)); comp.setStability(pickedDrop.stability.getRandom(random)); + ComponentsHandler.saveItemComponent(itemStack, comp); return itemStack; } public TurretDrop drawRandomTurret(QualityType type) { diff --git a/src/main/resources/assets/herbocraft/lang/en_us.json b/src/main/resources/assets/herbocraft/lang/en_us.json index c9660dd..03bc20f 100644 --- a/src/main/resources/assets/herbocraft/lang/en_us.json +++ b/src/main/resources/assets/herbocraft/lang/en_us.json @@ -32,6 +32,7 @@ "level_tooltips.stability.high": "High", "level_tooltips.stability.extreme": "Extreme", "level_tooltips.stability.unknown": "Unknown", + "level_tooltips.unknown": "The plant seems totally new to you.", "level_tooltips.sterile.true": "Sterile !", "upgrade.skipped_level": "Seems like you skipped a level ! Please apply the previous ones before" } \ No newline at end of file diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 97f0901..9ad0036 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -30,6 +30,6 @@ "depends": { "fabricloader": ">=0.8.7+build.201", "fabric": "*", - "minecraft": "1.16.1" + "minecraft": "1.16.2" } } diff --git a/src/main/resources/herbocraft.mixins.json b/src/main/resources/herbocraft.mixins.json index bbd5d85..0f1c9da 100644 --- a/src/main/resources/herbocraft.mixins.json +++ b/src/main/resources/herbocraft.mixins.json @@ -3,6 +3,7 @@ "package": "com.redblueflame.herbocraft.mixins", "compatibilityLevel": "JAVA_8", "mixins": [ + "FixTagInit" ], "client": [ "EntitySpawningHandlingMixin"