WIP Infuser

This commit is contained in:
TheBrokenRail 2020-07-29 22:51:29 -04:00
parent 8ef7f2dc11
commit 192fab32ff
18 changed files with 374 additions and 3 deletions

View File

@ -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) {

View File

@ -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));
}

View File

@ -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

View File

@ -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<Block, BlockState> builder) {
super.appendProperties(builder);
builder.add(POWERED);
}
@Override
protected Function<BlockEntityType<BlockEntity>, 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();
}
}
}

View File

@ -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);
}
}
}

View File

@ -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());
}
}
}

View File

@ -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<InfuserAction> weightedList = new WeightedList<>();
for (InfuserAction action : list) {
weightedList.add(1, action);
}
weightedList.pick(world.random).run(world, pos);
}
}

View File

@ -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<Item, InfuserEntry> 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<Item, InfuserEntry> 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()}));
}
}

View File

@ -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;
}

View File

@ -0,0 +1,10 @@
{
"variants": {
"powered=false": {
"model": "energonrelics:block/infuser_off"
},
"powered=true": {
"model": "energonrelics:block/infuser_on"
}
}
}

View File

@ -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"
}

View File

@ -0,0 +1,6 @@
{
"parent": "minecraft:block/cube_all",
"textures": {
"all": "energonrelics:block/infuser_off"
}
}

View File

@ -0,0 +1,6 @@
{
"parent": "minecraft:block/cube_all",
"textures": {
"all": "energonrelics:block/infuser_on"
}
}

View File

@ -0,0 +1,3 @@
{
"parent": "energonrelics:block/infuser_off"
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

View File

@ -0,0 +1,19 @@
{
"type": "minecraft:block",
"pools": [
{
"rolls": 1,
"entries": [
{
"type": "minecraft:item",
"name": "energonrelics:infuser"
}
],
"conditions": [
{
"condition": "minecraft:survives_explosion"
}
]
}
]
}

View File

@ -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
}
}