diff --git a/src/main/java/com/thebrokenrail/energonrelics/EnergonRelics.java b/src/main/java/com/thebrokenrail/energonrelics/EnergonRelics.java index 8804a48..1d33fb2 100644 --- a/src/main/java/com/thebrokenrail/energonrelics/EnergonRelics.java +++ b/src/main/java/com/thebrokenrail/energonrelics/EnergonRelics.java @@ -4,6 +4,7 @@ import com.thebrokenrail.energonrelics.block.BlockBreakerBlock; import com.thebrokenrail.energonrelics.block.CreativeEnergySourceBlock; import com.thebrokenrail.energonrelics.block.DefensiveLaserBlock; import com.thebrokenrail.energonrelics.block.HolographicSkyBlock; +import com.thebrokenrail.energonrelics.block.InfuserBlock; import com.thebrokenrail.energonrelics.block.lightning.LightningRodBaseBlock; import com.thebrokenrail.energonrelics.block.forcefield.ForcefieldProjectorBlock; import com.thebrokenrail.energonrelics.block.forcefield.laser.IndustrialLaserProjectorBlock; @@ -113,6 +114,8 @@ public class EnergonRelics implements ModInitializer { public static final HolographicSkyBlock HOLOGRAPHIC_SKY_BLOCK = new HolographicSkyBlock(); + public static final InfuserBlock INFUSER_BLOCK = new InfuserBlock(); + @Override public void onInitialize() { NETWORK_CHIP_ITEM = Registry.register(Registry.ITEM, new Identifier(NAMESPACE, "network_chip"), new NetworkChipItem()); @@ -170,6 +173,8 @@ public class EnergonRelics implements ModInitializer { CREATIVE_ENERGY_SOURCE_BLOCK.register("creative_energy_source"); HOLOGRAPHIC_SKY_BLOCK.register("holographic_sky"); + + INFUSER_BLOCK.register("infuser"); } public static void playBeep(World world, BlockPos pos) { diff --git a/src/main/java/com/thebrokenrail/energonrelics/block/BlockBreakerBlock.java b/src/main/java/com/thebrokenrail/energonrelics/block/BlockBreakerBlock.java index 8f47d38..bcd3d1d 100644 --- a/src/main/java/com/thebrokenrail/energonrelics/block/BlockBreakerBlock.java +++ b/src/main/java/com/thebrokenrail/energonrelics/block/BlockBreakerBlock.java @@ -21,7 +21,7 @@ public class BlockBreakerBlock extends FacingEnergyBlock { public static final BooleanProperty POWERED = Properties.POWERED; public BlockBreakerBlock() { - super(FabricBlockSettings.copy(Blocks.BLACKSTONE).strength(2f, 6f).nonOpaque().lightLevel(state -> state.get(POWERED) ? 7 : 0)); + super(FabricBlockSettings.copy(Blocks.POLISHED_BLACKSTONE_BRICKS).nonOpaque().lightLevel(state -> state.get(POWERED) ? 7 : 0)); setDefaultState(getDefaultState().with(POWERED, false)); } diff --git a/src/main/java/com/thebrokenrail/energonrelics/block/CreativeEnergySourceBlock.java b/src/main/java/com/thebrokenrail/energonrelics/block/CreativeEnergySourceBlock.java index ed0c4ce..606b6db 100644 --- a/src/main/java/com/thebrokenrail/energonrelics/block/CreativeEnergySourceBlock.java +++ b/src/main/java/com/thebrokenrail/energonrelics/block/CreativeEnergySourceBlock.java @@ -11,7 +11,7 @@ import java.util.function.Function; public class CreativeEnergySourceBlock extends EnergyBlock { public CreativeEnergySourceBlock() { - super(FabricBlockSettings.copy(Blocks.BEDROCK).dropsNothing()); + super(FabricBlockSettings.copy(Blocks.BEDROCK).dropsNothing().lightLevel(state -> 12)); } @Override diff --git a/src/main/java/com/thebrokenrail/energonrelics/block/InfuserBlock.java b/src/main/java/com/thebrokenrail/energonrelics/block/InfuserBlock.java new file mode 100644 index 0000000..8dbeced --- /dev/null +++ b/src/main/java/com/thebrokenrail/energonrelics/block/InfuserBlock.java @@ -0,0 +1,78 @@ +package com.thebrokenrail.energonrelics.block; + +import com.thebrokenrail.energonrelics.block.entity.infuser.InfuserBlockEntity; +import com.thebrokenrail.energonrelics.block.util.energy.EnergyBlock; +import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; +import net.minecraft.block.Block; +import net.minecraft.block.BlockState; +import net.minecraft.block.Blocks; +import net.minecraft.block.entity.BlockEntity; +import net.minecraft.block.entity.BlockEntityType; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.ItemStack; +import net.minecraft.state.StateManager; +import net.minecraft.state.property.BooleanProperty; +import net.minecraft.state.property.Properties; +import net.minecraft.util.ActionResult; +import net.minecraft.util.Hand; +import net.minecraft.util.hit.BlockHitResult; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; + +import java.util.function.Function; + +public class InfuserBlock extends EnergyBlock { + public static final BooleanProperty POWERED = Properties.POWERED; + + public InfuserBlock() { + super(FabricBlockSettings.copy(Blocks.GOLD_BLOCK).nonOpaque().lightLevel(state -> state.get(POWERED) ? 7 : 0).emissiveLighting((state, world, pos) -> state.get(POWERED))); + setDefaultState(getDefaultState().with(POWERED, false)); + } + + @Override + protected void appendProperties(StateManager.Builder builder) { + super.appendProperties(builder); + builder.add(POWERED); + } + + @Override + protected Function, BlockEntity> getFactory() { + return InfuserBlockEntity::new; + } + + @Override + public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { + boolean success; + + BlockEntity entity = world.getBlockEntity(pos); + if (entity instanceof InfuserBlockEntity) { + ItemStack stack = player.getStackInHand(hand); + if (((InfuserBlockEntity) entity).insert(stack.getItem())) { + if (!player.isCreative()) { + stack.decrement(1); + } + success = true; + } else { + success = false; + } + } else { + success = false; + } + + if (success) { + return ActionResult.SUCCESS; + } else { + return super.onUse(state, world, pos, player, hand, hit); + } + } + + @Override + public void onBreak(World world, BlockPos pos, BlockState state, PlayerEntity player) { + super.onBreak(world, pos, state, player); + + BlockEntity entity = world.getBlockEntity(pos); + if (entity instanceof InfuserBlockEntity) { + ((InfuserBlockEntity) entity).onBreak(); + } + } +} diff --git a/src/main/java/com/thebrokenrail/energonrelics/block/entity/infuser/InfuserAction.java b/src/main/java/com/thebrokenrail/energonrelics/block/entity/infuser/InfuserAction.java new file mode 100644 index 0000000..4723d5e --- /dev/null +++ b/src/main/java/com/thebrokenrail/energonrelics/block/entity/infuser/InfuserAction.java @@ -0,0 +1,38 @@ +package com.thebrokenrail.energonrelics.block.entity.infuser; + +import net.minecraft.block.Block; +import net.minecraft.block.Blocks; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; +import net.minecraft.world.explosion.Explosion; + +interface InfuserAction { + void run(World world, BlockPos pos); + + class ItemAction implements InfuserAction { + private final ItemStack stack; + + ItemAction(ItemStack stack) { + this.stack = stack; + } + + ItemAction(Item item) { + this(new ItemStack(item)); + } + + @Override + public void run(World world, BlockPos pos) { + Block.dropStack(world, pos, stack); + } + } + + class ExplosionAction implements InfuserAction { + @Override + public void run(World world, BlockPos pos) { + world.setBlockState(pos, Blocks.AIR.getDefaultState()); + world.createExplosion(null, pos.getX() + 0.5d, pos.getY() + 0.5d, pos.getZ() + 0.5d, 3f, true, Explosion.DestructionType.DESTROY); + } + } +} diff --git a/src/main/java/com/thebrokenrail/energonrelics/block/entity/infuser/InfuserBlockEntity.java b/src/main/java/com/thebrokenrail/energonrelics/block/entity/infuser/InfuserBlockEntity.java new file mode 100644 index 0000000..31c0c73 --- /dev/null +++ b/src/main/java/com/thebrokenrail/energonrelics/block/entity/infuser/InfuserBlockEntity.java @@ -0,0 +1,97 @@ +package com.thebrokenrail.energonrelics.block.entity.infuser; + +import com.thebrokenrail.energonrelics.block.InfuserBlock; +import com.thebrokenrail.energonrelics.config.HardcodedConfig; +import com.thebrokenrail.energonrelics.energy.core.EnergyReceiverBlockEntity; +import com.thebrokenrail.energonrelics.energy.core.util.Action; +import net.minecraft.block.BlockState; +import net.minecraft.block.entity.BlockEntityType; +import net.minecraft.item.Item; +import net.minecraft.nbt.CompoundTag; + +import java.util.Objects; + +public class InfuserBlockEntity extends EnergyReceiverBlockEntity { + public InfuserBlockEntity(BlockEntityType type) { + super(type); + } + + private InfuserEntry entry = null; + private int progress = 0; + + private static final InfuserAction explosion = new InfuserAction.ExplosionAction(); + + @Override + protected void energyTick() { + assert getWorld() != null; + + if (getCachedState().get(InfuserBlock.POWERED)) { + if (entry != null) { + if (progress >= HardcodedConfig.INFUSER_TIME) { + getWorld().setBlockState(getPos(), getCachedState().with(InfuserBlock.POWERED, false)); + + entry.run(Objects.requireNonNull(getWorld()), getPos()); + + progress = 0; + entry = null; + markDirty(); + } else { + addAction(new Action(entry.cost, (world, pos, state) -> { + progress++; + markDirty(); + }, (world, pos, state) -> explosion.run(world, pos))); + } + } else { + explosion.run(getWorld(), getPos()); + } + } else { + if (progress != 0 || entry != null) { + progress = 0; + entry = null; + markDirty(); + } + } + } + + @Override + public CompoundTag toTag(CompoundTag tag) { + super.toTag(tag); + tag.putString("Entry", InfuserRegistry.toString(entry)); + tag.putInt("Progress", progress); + return tag; + } + + @Override + public void fromTag(BlockState state, CompoundTag tag) { + super.fromTag(state, tag); + entry = InfuserRegistry.fromString(tag.getString("Entry")); + progress = tag.getInt("Progress"); + } + + public boolean insert(Item item) { + assert getWorld() != null; + + if (getCachedState().get(InfuserBlock.POWERED)) { + return false; + } else { + InfuserEntry newEntry = InfuserRegistry.get(item); + if (newEntry != null) { + if (!getWorld().isClient()) { + getWorld().setBlockState(getPos(), getCachedState().with(InfuserBlock.POWERED, true)); + entry = newEntry; + progress = 0; + markDirty(); + } + return true; + } else { + return false; + } + } + } + + public void onBreak() { + if (getCachedState().get(InfuserBlock.POWERED)) { + explosion.run(getWorld(), getPos()); + } + } +} diff --git a/src/main/java/com/thebrokenrail/energonrelics/block/entity/infuser/InfuserEntry.java b/src/main/java/com/thebrokenrail/energonrelics/block/entity/infuser/InfuserEntry.java new file mode 100644 index 0000000..6779e60 --- /dev/null +++ b/src/main/java/com/thebrokenrail/energonrelics/block/entity/infuser/InfuserEntry.java @@ -0,0 +1,37 @@ +package com.thebrokenrail.energonrelics.block.entity.infuser; + +import com.thebrokenrail.energonrelics.util.WeightedList; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; + +class InfuserEntry { + public final long cost; + public final double successChance; + public final InfuserAction[] success; + public final InfuserAction[] fail; + + InfuserEntry(long cost, double successChance, InfuserAction[] success, InfuserAction[] fail) { + this.cost = cost; + this.successChance = successChance; + this.success = success; + this.fail = fail; + } + + void run(World world, BlockPos pos) { + boolean isSuccess = world.random.nextDouble() <= successChance; + + InfuserAction[] list; + if (isSuccess) { + list = success; + } else { + list = fail; + } + + WeightedList weightedList = new WeightedList<>(); + for (InfuserAction action : list) { + weightedList.add(1, action); + } + + weightedList.pick(world.random).run(world, pos); + } +} diff --git a/src/main/java/com/thebrokenrail/energonrelics/block/entity/infuser/InfuserRegistry.java b/src/main/java/com/thebrokenrail/energonrelics/block/entity/infuser/InfuserRegistry.java new file mode 100644 index 0000000..3469dbb --- /dev/null +++ b/src/main/java/com/thebrokenrail/energonrelics/block/entity/infuser/InfuserRegistry.java @@ -0,0 +1,46 @@ +package com.thebrokenrail.energonrelics.block.entity.infuser; + +import net.minecraft.item.Item; +import net.minecraft.item.Items; +import net.minecraft.util.Identifier; +import net.minecraft.util.registry.Registry; + +import java.util.HashMap; +import java.util.Map; + +public class InfuserRegistry { + private static final Map map = new HashMap<>(); + + static InfuserEntry get(Item item) { + return map.get(item); + } + + private static void add(Item item, InfuserEntry entry) { + map.put(item, entry); + } + + static String toString(InfuserEntry entry) { + Item item = null; + for (Map.Entry mapEntry : map.entrySet()) { + if (mapEntry.getValue() == entry) { + item = mapEntry.getKey(); + break; + } + } + + if (item != null) { + return Registry.ITEM.getId(item).toString(); + } else { + return ""; + } + } + + static InfuserEntry fromString(String str) { + Item item = Registry.ITEM.get(new Identifier(str)); + return map.getOrDefault(item, null); + } + + static { + add(Items.SUGAR, new InfuserEntry(51, 0.76d, new InfuserAction[]{new InfuserAction.ItemAction(Items.GLOWSTONE_DUST)}, new InfuserAction[]{new InfuserAction.ItemAction(Items.SUGAR_CANE), new InfuserAction.ExplosionAction()})); + } +} diff --git a/src/main/java/com/thebrokenrail/energonrelics/config/HardcodedConfig.java b/src/main/java/com/thebrokenrail/energonrelics/config/HardcodedConfig.java index 3f50750..48928e9 100644 --- a/src/main/java/com/thebrokenrail/energonrelics/config/HardcodedConfig.java +++ b/src/main/java/com/thebrokenrail/energonrelics/config/HardcodedConfig.java @@ -42,4 +42,6 @@ public class HardcodedConfig { public static final int INDUSTRIAL_LASER_INGOTS_FROM_STORAGE = 9; public static final long HOLOGRAPHIC_SKY_ENERGY_REQUIRED = 15; + + public static final int INFUSER_TIME = 160; } \ No newline at end of file diff --git a/src/main/resources/assets/energonrelics/blockstates/infuser.json b/src/main/resources/assets/energonrelics/blockstates/infuser.json new file mode 100644 index 0000000..02c09e2 --- /dev/null +++ b/src/main/resources/assets/energonrelics/blockstates/infuser.json @@ -0,0 +1,10 @@ +{ + "variants": { + "powered=false": { + "model": "energonrelics:block/infuser_off" + }, + "powered=true": { + "model": "energonrelics:block/infuser_on" + } + } +} diff --git a/src/main/resources/assets/energonrelics/lang/en_us.json b/src/main/resources/assets/energonrelics/lang/en_us.json index fcfa0b7..7064442 100644 --- a/src/main/resources/assets/energonrelics/lang/en_us.json +++ b/src/main/resources/assets/energonrelics/lang/en_us.json @@ -45,5 +45,6 @@ "block.energonrelics.industrial_laser_projector": "Industrial Laser Projector", "block.energonrelics.industrial_laser": "Industrial Laser", "death.attack.energonrelics.industrial_laser": "%s was melted by an industrial laser", - "death.attack.energonrelics.industrial_laser.player": "%s was melted by an industrial laser whilst fighting %s" + "death.attack.energonrelics.industrial_laser.player": "%s was melted by an industrial laser whilst fighting %s", + "block.energonrelics.infuser": "Infuser" } \ No newline at end of file diff --git a/src/main/resources/assets/energonrelics/models/block/infuser_off.json b/src/main/resources/assets/energonrelics/models/block/infuser_off.json new file mode 100644 index 0000000..a520bc1 --- /dev/null +++ b/src/main/resources/assets/energonrelics/models/block/infuser_off.json @@ -0,0 +1,6 @@ +{ + "parent": "minecraft:block/cube_all", + "textures": { + "all": "energonrelics:block/infuser_off" + } +} diff --git a/src/main/resources/assets/energonrelics/models/block/infuser_on.json b/src/main/resources/assets/energonrelics/models/block/infuser_on.json new file mode 100644 index 0000000..8cc43f8 --- /dev/null +++ b/src/main/resources/assets/energonrelics/models/block/infuser_on.json @@ -0,0 +1,6 @@ +{ + "parent": "minecraft:block/cube_all", + "textures": { + "all": "energonrelics:block/infuser_on" + } +} diff --git a/src/main/resources/assets/energonrelics/models/item/infuser.json b/src/main/resources/assets/energonrelics/models/item/infuser.json new file mode 100644 index 0000000..f0d4377 --- /dev/null +++ b/src/main/resources/assets/energonrelics/models/item/infuser.json @@ -0,0 +1,3 @@ +{ + "parent": "energonrelics:block/infuser_off" +} diff --git a/src/main/resources/assets/energonrelics/textures/block/infuser_off.png b/src/main/resources/assets/energonrelics/textures/block/infuser_off.png new file mode 100644 index 0000000..0989bde Binary files /dev/null and b/src/main/resources/assets/energonrelics/textures/block/infuser_off.png differ diff --git a/src/main/resources/assets/energonrelics/textures/block/infuser_on.png b/src/main/resources/assets/energonrelics/textures/block/infuser_on.png new file mode 100644 index 0000000..f602d92 Binary files /dev/null and b/src/main/resources/assets/energonrelics/textures/block/infuser_on.png differ diff --git a/src/main/resources/data/energonrelics/loot_tables/blocks/infuser.json b/src/main/resources/data/energonrelics/loot_tables/blocks/infuser.json new file mode 100644 index 0000000..90453d2 --- /dev/null +++ b/src/main/resources/data/energonrelics/loot_tables/blocks/infuser.json @@ -0,0 +1,19 @@ +{ + "type": "minecraft:block", + "pools": [ + { + "rolls": 1, + "entries": [ + { + "type": "minecraft:item", + "name": "energonrelics:infuser" + } + ], + "conditions": [ + { + "condition": "minecraft:survives_explosion" + } + ] + } + ] +} \ No newline at end of file diff --git a/src/main/resources/data/energonrelics/recipes/infuser.json b/src/main/resources/data/energonrelics/recipes/infuser.json new file mode 100644 index 0000000..d3a184e --- /dev/null +++ b/src/main/resources/data/energonrelics/recipes/infuser.json @@ -0,0 +1,23 @@ +{ + "type": "minecraft:crafting_shaped", + "pattern": [ + "#G#", + "GVG", + "#G#" + ], + "key": { + "#": { + "item": "energonrelics:circuit_board" + }, + "G": { + "item": "minecraft:gold_ingot" + }, + "V": { + "item": "energonrelics:veridium_ingot" + } + }, + "result": { + "item": "energonrelics:infuser", + "count": 1 + } +} \ No newline at end of file