WIP: smelter

This commit is contained in:
Aleksey 2020-09-28 17:29:42 +03:00
parent 0373b4c116
commit aca21ae138
10 changed files with 520 additions and 44 deletions

View file

@ -1,6 +1,7 @@
package ru.betterend; package ru.betterend;
import net.fabricmc.api.ModInitializer; import net.fabricmc.api.ModInitializer;
import ru.betterend.config.MainConfig; import ru.betterend.config.MainConfig;
import ru.betterend.recipe.CraftingRecipes; import ru.betterend.recipe.CraftingRecipes;
import ru.betterend.registry.BiomeRegistry; import ru.betterend.registry.BiomeRegistry;

View file

@ -7,6 +7,7 @@ import java.util.Random;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.BlockRenderType; import net.minecraft.block.BlockRenderType;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
@ -19,6 +20,7 @@ import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.loot.context.LootContext; import net.minecraft.loot.context.LootContext;
import net.minecraft.particle.ParticleTypes; import net.minecraft.particle.ParticleTypes;
import net.minecraft.screen.NamedScreenHandlerFactory;
import net.minecraft.screen.ScreenHandler; import net.minecraft.screen.ScreenHandler;
import net.minecraft.sound.BlockSoundGroup; import net.minecraft.sound.BlockSoundGroup;
import net.minecraft.sound.SoundCategory; import net.minecraft.sound.SoundCategory;
@ -36,11 +38,14 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction; import net.minecraft.util.math.Direction;
import net.minecraft.world.BlockView; import net.minecraft.world.BlockView;
import net.minecraft.world.World; import net.minecraft.world.World;
import ru.betterend.blocks.basis.BaseBlockWithEntity; import ru.betterend.blocks.basis.BaseBlockWithEntity;
import ru.betterend.blocks.entities.EndStoneSmelterBlockEntity;
public class EndStoneSmelter extends BaseBlockWithEntity { public class EndStoneSmelter extends BaseBlockWithEntity {
public static final DirectionProperty FACING = HorizontalFacingBlock.FACING; public static final DirectionProperty FACING = HorizontalFacingBlock.FACING;
public static final BooleanProperty LIT = Properties.LIT; public static final BooleanProperty LIT = Properties.LIT;
public static final String ID = "end_stone_smelter";
public EndStoneSmelter() { public EndStoneSmelter() {
super(FabricBlockSettings.of(Material.STONE, MaterialColor.GRAY) super(FabricBlockSettings.of(Material.STONE, MaterialColor.GRAY)
@ -63,7 +68,10 @@ public class EndStoneSmelter extends BaseBlockWithEntity {
} }
private void openScreen(World world, BlockPos pos, PlayerEntity player) { private void openScreen(World world, BlockPos pos, PlayerEntity player) {
BlockEntity blockEntity = world.getBlockEntity(pos);
if (blockEntity instanceof EndStoneSmelterBlockEntity) {
player.openHandledScreen((NamedScreenHandlerFactory) blockEntity);
}
} }
@Override @Override
@ -73,7 +81,7 @@ public class EndStoneSmelter extends BaseBlockWithEntity {
@Override @Override
public BlockEntity createBlockEntity(BlockView world) { public BlockEntity createBlockEntity(BlockView world) {
return null; return new EndStoneSmelterBlockEntity();
} }
@Override @Override

View file

@ -1,19 +1,26 @@
package ru.betterend.blocks.entities; package ru.betterend.blocks.entities;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
import java.util.Map; import java.util.Map;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectIterator;
import it.unimi.dsi.fastutil.objects.Object2IntMap.Entry;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.block.entity.LockableContainerBlockEntity; import net.minecraft.block.entity.LockableContainerBlockEntity;
import net.minecraft.entity.ExperienceOrbEntity;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory; import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventories; import net.minecraft.inventory.Inventories;
import net.minecraft.inventory.SidedInventory; import net.minecraft.inventory.SidedInventory;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.recipe.Recipe; import net.minecraft.recipe.Recipe;
import net.minecraft.recipe.RecipeFinder; import net.minecraft.recipe.RecipeFinder;
@ -23,27 +30,39 @@ import net.minecraft.screen.PropertyDelegate;
import net.minecraft.screen.ScreenHandler; import net.minecraft.screen.ScreenHandler;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText; import net.minecraft.text.TranslatableText;
import net.minecraft.util.Identifier;
import net.minecraft.util.Tickable; import net.minecraft.util.Tickable;
import net.minecraft.util.collection.DefaultedList; import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.math.Direction; import net.minecraft.util.math.Direction;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import ru.betterend.BetterEnd; import ru.betterend.BetterEnd;
import ru.betterend.blocks.EndStoneSmelter;
import ru.betterend.client.gui.EndStoneSmelterScreenHandler;
import ru.betterend.recipe.AlloyingRecipe;
import ru.betterend.registry.BlockEntityRegistry;
public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity implements SidedInventory, RecipeUnlocker, RecipeInputProvider, Tickable { public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity implements SidedInventory, RecipeUnlocker, RecipeInputProvider, Tickable {
private static final int[] TOP_SLOTS = new int[] { 0, 1 }; private static final int[] TOP_SLOTS = new int[] { 0, 1 };
private static final int[] BOTTOM_SLOTS = new int[] { 2, 3 }; private static final int[] BOTTOM_SLOTS = new int[] { 2, 3 };
private static final int[] SIDE_SLOTS = new int[] { 3 }; private static final int[] SIDE_SLOTS = new int[] { 3 };
private static final Map<Item, Integer> availableFuels = Maps.newHashMap();
private final Object2IntOpenHashMap<Identifier> recipesUsed;
protected DefaultedList<ItemStack> inventory; protected DefaultedList<ItemStack> inventory;
protected final PropertyDelegate propertyDelegate; protected final PropertyDelegate propertyDelegate;
private Map<Item, Integer> availableFuels = Maps.newHashMap();
private int burnTime; private int burnTime;
private int fuelTime; private int fuelTime;
private int smeltTime; private int smeltTime;
private int smeltTimeTotal; private int smeltTimeTotal;
protected EndStoneSmelterBlockEntity(BlockEntityType<?> blockEntityType) { public EndStoneSmelterBlockEntity() {
super(blockEntityType); super(BlockEntityRegistry.END_STONE_SMELTER);
this.inventory = DefaultedList.ofSize(4, ItemStack.EMPTY); this.inventory = DefaultedList.ofSize(4, ItemStack.EMPTY);
this.recipesUsed = new Object2IntOpenHashMap<Identifier>();
this.propertyDelegate = new PropertyDelegate() { this.propertyDelegate = new PropertyDelegate() {
public int get(int index) { public int get(int index) {
switch(index) { switch(index) {
@ -137,8 +156,37 @@ public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity imp
} }
protected int getSmeltTime() { protected int getSmeltTime() {
//TODO return this.world.getRecipeManager().getFirstMatch(AlloyingRecipe.TYPE, this, world)
return 0; .map(AlloyingRecipe::getSmeltTime).orElse(350);
}
public void dropExperience(PlayerEntity player) {
List<Recipe<?>> list = Lists.newArrayList();
ObjectIterator<Entry<Identifier>> usedRecipes = this.recipesUsed.object2IntEntrySet().iterator();
while(usedRecipes.hasNext()) {
Entry<Identifier> entry = usedRecipes.next();
world.getRecipeManager().get(entry.getKey()).ifPresent((recipe) -> {
list.add(recipe);
AlloyingRecipe alloying = (AlloyingRecipe) recipe;
this.dropExperience(player.world, player.getPos(), entry.getIntValue(), alloying.getExperience());
});
}
player.unlockRecipes(list);
this.recipesUsed.clear();
}
private void dropExperience(World world, Vec3d vec3d, int i, float f) {
int j = MathHelper.floor(i * f);
float g = MathHelper.fractionalPart(i * f);
if (g != 0.0F && Math.random() < g) {
j++;
}
while(j > 0) {
int k = ExperienceOrbEntity.roundToOrbSize(j);
j -= k;
world.spawnEntity(new ExperienceOrbEntity(world, vec3d.x, vec3d.y, vec3d.z, k));
}
} }
@Override @Override
@ -157,55 +205,63 @@ public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity imp
@Override @Override
protected Text getContainerName() { protected Text getContainerName() {
return new TranslatableText(String.format("block.%s.end_stone_smelter", BetterEnd.MOD_ID)); return new TranslatableText(String.format("block.%s.%s", BetterEnd.MOD_ID, EndStoneSmelter.ID));
} }
@Override @Override
protected ScreenHandler createScreenHandler(int syncId, PlayerInventory playerInventory) { protected ScreenHandler createScreenHandler(int syncId, PlayerInventory playerInventory) {
// TODO Auto-generated method stub return new EndStoneSmelterScreenHandler(syncId, playerInventory, this, propertyDelegate);
return null;
} }
@Override @Override
public void tick() { public void tick() {
// TODO Auto-generated method stub // TODO Auto-generated method stub
} }
@Override @Override
public void provideRecipeInputs(RecipeFinder finder) { public void provideRecipeInputs(RecipeFinder finder) {
// TODO Auto-generated method stub Iterator<ItemStack> inventory = this.inventory.iterator();
while(inventory.hasNext()) {
ItemStack itemStack = inventory.next();
finder.addItem(itemStack);
}
} }
@Override @Override
public void setLastRecipe(Recipe<?> recipe) { public void setLastRecipe(Recipe<?> recipe) {
// TODO Auto-generated method stub if (recipe != null) {
Identifier recipeId = recipe.getId();
this.recipesUsed.addTo(recipeId, 1);
}
} }
@Override @Override
public Recipe<?> getLastRecipe() { public Recipe<?> getLastRecipe() {
// TODO Auto-generated method stub
return null; return null;
} }
@Override @Override
public int[] getAvailableSlots(Direction side) { public int[] getAvailableSlots(Direction side) {
// TODO Auto-generated method stub if (side == Direction.DOWN) {
return null; return BOTTOM_SLOTS;
} else {
return side == Direction.UP ? TOP_SLOTS : SIDE_SLOTS;
}
} }
@Override @Override
public boolean canInsert(int slot, ItemStack stack, Direction dir) { public boolean canInsert(int slot, ItemStack stack, Direction dir) {
// TODO Auto-generated method stub return this.isValid(slot, stack);
return false;
} }
@Override @Override
public boolean canExtract(int slot, ItemStack stack, Direction dir) { public boolean canExtract(int slot, ItemStack stack, Direction dir) {
// TODO Auto-generated method stub if (dir == Direction.DOWN && slot == 2) {
return false; if (stack.getItem() != Items.BUCKET) {
return false;
}
}
return true;
} }
protected int getFuelTime(ItemStack fuel) { protected int getFuelTime(ItemStack fuel) {
@ -213,7 +269,7 @@ public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity imp
return 0; return 0;
} else { } else {
Item item = fuel.getItem(); Item item = fuel.getItem();
return this.availableFuels.getOrDefault(item, 0); return availableFuels.getOrDefault(item, 0);
} }
} }
@ -226,6 +282,12 @@ public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity imp
this.smeltTime = tag.getShort("SmeltTime"); this.smeltTime = tag.getShort("SmeltTime");
this.smeltTimeTotal = tag.getShort("SmeltTimeTotal"); this.smeltTimeTotal = tag.getShort("SmeltTimeTotal");
this.fuelTime = this.getFuelTime(this.inventory.get(2)); this.fuelTime = this.getFuelTime(this.inventory.get(2));
CompoundTag compoundTag = tag.getCompound("RecipesUsed");
Iterator<String> recipes = compoundTag.getKeys().iterator();
while(recipes.hasNext()) {
String id = recipes.next();
this.recipesUsed.put(new Identifier(id), compoundTag.getInt(id));
}
} }
@Override @Override
@ -235,6 +297,27 @@ public class EndStoneSmelterBlockEntity extends LockableContainerBlockEntity imp
tag.putShort("SmeltTime", (short)this.smeltTime); tag.putShort("SmeltTime", (short)this.smeltTime);
tag.putShort("SmeltTimeTotal", (short)this.smeltTimeTotal); tag.putShort("SmeltTimeTotal", (short)this.smeltTimeTotal);
Inventories.toTag(tag, this.inventory); Inventories.toTag(tag, this.inventory);
CompoundTag usedRecipes = new CompoundTag();
this.recipesUsed.forEach((identifier, integer) -> {
usedRecipes.putInt(identifier.toString(), integer);
});
tag.put("RecipesUsed", usedRecipes);
return tag; return tag;
} }
public boolean isValid(int slot, ItemStack stack) {
if (slot == 3) {
return false;
} else if (slot != 0 || slot != 1) {
return true;
} else {
ItemStack itemStack = this.inventory.get(2);
return canUseAsFuel(stack) || stack.getItem() == Items.BUCKET && itemStack.getItem() != Items.BUCKET;
}
}
public static boolean canUseAsFuel(ItemStack stack) {
return availableFuels.containsKey(stack.getItem());
}
} }

View file

@ -0,0 +1,139 @@
package ru.betterend.client.gui;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.screenhandler.v1.ScreenHandlerRegistry;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.SimpleInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.recipe.Recipe;
import net.minecraft.recipe.RecipeFinder;
import net.minecraft.recipe.RecipeInputProvider;
import net.minecraft.recipe.book.RecipeBookCategory;
import net.minecraft.screen.AbstractRecipeScreenHandler;
import net.minecraft.screen.ArrayPropertyDelegate;
import net.minecraft.screen.PropertyDelegate;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.screen.slot.Slot;
import net.minecraft.util.Identifier;
import net.minecraft.world.World;
import ru.betterend.BetterEnd;
import ru.betterend.blocks.EndStoneSmelter;
import ru.betterend.blocks.entities.EndStoneSmelterBlockEntity;
import ru.betterend.recipe.AlloyingRecipe;
public class EndStoneSmelterScreenHandler extends AbstractRecipeScreenHandler<Inventory> {
public final static ScreenHandlerType<EndStoneSmelterScreenHandler> HANDLER_TYPE = ScreenHandlerRegistry.registerSimple(
new Identifier(BetterEnd.MOD_ID, EndStoneSmelter.ID), EndStoneSmelterScreenHandler::new);
private final Inventory inventory;
private final PropertyDelegate propertyDelegate;
protected final World world;
public EndStoneSmelterScreenHandler(int syncId, PlayerInventory playerInventory) {
this(syncId, playerInventory, new SimpleInventory(4), new ArrayPropertyDelegate(4));
}
public EndStoneSmelterScreenHandler(int syncId, PlayerInventory playerInventory, Inventory inventory, PropertyDelegate propertyDelegate) {
super(HANDLER_TYPE, syncId);
this.inventory = inventory;
this.propertyDelegate = propertyDelegate;
this.world = playerInventory.player.world;
this.addProperties(propertyDelegate);
this.addSlot(new Slot(inventory, 0, 52, 17));
this.addSlot(new Slot(inventory, 1, 77, 17));
this.addSlot(new SmelterFuelSlot(this, inventory, 2, 56, 53));
this.addSlot(new SmelterOutputSlot(playerInventory.player, inventory, 3, 116, 35));
for(int i = 0; i < 3; ++i) {
for(int j = 0; j < 9; ++j) {
this.addSlot(new Slot(playerInventory, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
}
}
for(int i = 0; i < 9; ++i) {
this.addSlot(new Slot(playerInventory, i, 8 + i * 18, 142));
}
}
@Override
public void populateRecipeFinder(RecipeFinder finder) {
if (inventory instanceof RecipeInputProvider) {
((RecipeInputProvider) inventory).provideRecipeInputs(finder);
}
}
@Override
public void clearCraftingSlots() {
this.inventory.clear();
}
@Override
public boolean matches(Recipe<? super Inventory> recipe) {
return recipe.matches(this.inventory, this.world);
}
@Override
public int getCraftingResultSlotIndex() {
return 3;
}
@Override
public int getCraftingWidth() {
return 2;
}
@Override
public int getCraftingHeight() {
return 1;
}
@Override
public int getCraftingSlotCount() {
return 4;
}
@Override
public RecipeBookCategory getCategory() {
return RecipeBookCategory.FURNACE;
}
@Override
public boolean canUse(PlayerEntity player) {
return this.inventory.canPlayerUse(player);
}
protected boolean isSmeltable(ItemStack itemStack) {
return this.world.getRecipeManager().getFirstMatch(AlloyingRecipe.TYPE, new SimpleInventory(new ItemStack[]{itemStack}), this.world).isPresent();
}
protected boolean isFuel(ItemStack itemStack) {
return EndStoneSmelterBlockEntity.canUseAsFuel(itemStack);
}
@Environment(EnvType.CLIENT)
public int getSmeltProgress() {
int time = this.propertyDelegate.get(2);
int timeTotal = this.propertyDelegate.get(3);
return timeTotal != 0 && time != 0 ? time * 24 / timeTotal : 0;
}
@Environment(EnvType.CLIENT)
public int getFuelProgress() {
int fuelTime = this.propertyDelegate.get(1);
if (fuelTime == 0) {
fuelTime = 200;
}
return this.propertyDelegate.get(0) * 13 / fuelTime;
}
@Environment(EnvType.CLIENT)
public boolean isBurning() {
return this.propertyDelegate.get(0) > 0;
}
}

View file

@ -0,0 +1,24 @@
package ru.betterend.client.gui;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.slot.FurnaceFuelSlot;
import net.minecraft.screen.slot.Slot;
public class SmelterFuelSlot extends Slot {
private final EndStoneSmelterScreenHandler handler;
public SmelterFuelSlot(EndStoneSmelterScreenHandler handler, Inventory inventory, int index, int x, int y) {
super(inventory, index, x, y);
this.handler = handler;
}
public boolean canInsert(ItemStack stack) {
return this.handler.isFuel(stack) || FurnaceFuelSlot.isBucket(stack);
}
public int getMaxItemCount(ItemStack stack) {
return FurnaceFuelSlot.isBucket(stack) ? 1 : super.getMaxItemCount(stack);
}
}

View file

@ -0,0 +1,50 @@
package ru.betterend.client.gui;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.slot.Slot;
import ru.betterend.blocks.entities.EndStoneSmelterBlockEntity;
public class SmelterOutputSlot extends Slot {
private PlayerEntity player;
private int amount;
public SmelterOutputSlot(PlayerEntity player, Inventory inventory, int index, int x, int y) {
super(inventory, index, x, y);
this.player = player;
}
public boolean canInsert(ItemStack stack) {
return false;
}
public ItemStack takeStack(int amount) {
if (this.hasStack()) {
this.amount += Math.min(amount, this.getStack().getCount());
}
return super.takeStack(amount);
}
public ItemStack onTakeItem(PlayerEntity player, ItemStack stack) {
this.onCrafted(stack);
super.onTakeItem(player, stack);
return stack;
}
protected void onCrafted(ItemStack stack, int amount) {
this.amount += amount;
this.onCrafted(stack);
}
protected void onCrafted(ItemStack stack) {
stack.onCraft(this.player.world, this.player, this.amount);
if (!this.player.world.isClient && this.inventory instanceof EndStoneSmelterBlockEntity) {
((EndStoneSmelterBlockEntity) this.inventory).dropExperience(player);
}
this.amount = 0;
}
}

View file

@ -0,0 +1,118 @@
package ru.betterend.recipe;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.recipe.Ingredient;
import net.minecraft.recipe.Recipe;
import net.minecraft.recipe.RecipeSerializer;
import net.minecraft.recipe.RecipeType;
import net.minecraft.util.Identifier;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.World;
import ru.betterend.BetterEnd;
import ru.betterend.registry.BlockRegistry;
public class AlloyingRecipe implements Recipe<Inventory> {
public final static String GROUP = "alloying";
public final static RecipeType<AlloyingRecipe> TYPE = registerType(GROUP);
public final static AlloyingRecipeSerializer SERIALIZER = registerSerializer(GROUP, new AlloyingRecipeSerializer());
protected final RecipeType<?> type;
protected final Identifier id;
protected final Ingredient primaryInput;
protected final Ingredient secondaryInput;
protected final ItemStack output;
protected final float experience;
protected final int smeltTime;
public AlloyingRecipe(Identifier id, Ingredient primaryInput, Ingredient secondaryInput,
ItemStack output, float experience, int smeltTime) {
this.id = id;
this.primaryInput = primaryInput;
this.secondaryInput = secondaryInput;
this.output = output;
this.experience = experience;
this.smeltTime = smeltTime;
this.type = TYPE;
}
public float getExperience() {
return this.experience;
}
public int getSmeltTime() {
return this.smeltTime;
}
public DefaultedList<Ingredient> getPreviewInputs() {
DefaultedList<Ingredient> defaultedList = DefaultedList.of();
defaultedList.add(primaryInput);
defaultedList.add(secondaryInput);
return defaultedList;
}
@Override
public boolean matches(Inventory inv, World world) {
return this.primaryInput.test(inv.getStack(0)) && this.secondaryInput.test(inv.getStack(1)) ||
this.primaryInput.test(inv.getStack(1)) && this.secondaryInput.test(inv.getStack(0));
}
@Override
public ItemStack craft(Inventory inv) {
return this.output.copy();
}
@Override
public boolean fits(int width, int height) {
return true;
}
@Override
public ItemStack getOutput() {
return this.output;
}
@Override
public Identifier getId() {
return this.id;
}
@Override
public RecipeSerializer<?> getSerializer() {
return SERIALIZER;
}
@Override
public RecipeType<?> getType() {
return this.type;
}
@Environment(EnvType.CLIENT)
public String getGroup() {
return GROUP;
}
@Environment(EnvType.CLIENT)
public ItemStack getRecipeKindIcon() {
return new ItemStack(BlockRegistry.END_STONE_SMELTER);
}
private static <S extends RecipeSerializer<T>, T extends Recipe<?>> S registerSerializer(String id, S serializer) {
return Registry.register(Registry.RECIPE_SERIALIZER, new Identifier(BetterEnd.MOD_ID, id), serializer);
}
private static <T extends Recipe<?>> RecipeType<T> registerType(String name) {
return Registry.register(Registry.RECIPE_TYPE, new Identifier(BetterEnd.MOD_ID, name), new RecipeType<T>() {
public String toString() {
return name;
}
});
}
}

View file

@ -0,0 +1,52 @@
package ru.betterend.recipe;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.recipe.Ingredient;
import net.minecraft.recipe.RecipeSerializer;
import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper;
import net.minecraft.util.registry.Registry;
public class AlloyingRecipeSerializer implements RecipeSerializer<AlloyingRecipe> {
@Override
public AlloyingRecipe read(Identifier id, JsonObject json) {
JsonArray ingredients = JsonHelper.getArray(json, "ingredients");
Ingredient primaryInput = Ingredient.fromJson(ingredients.get(0));
Ingredient secondaryInput = Ingredient.fromJson(ingredients.get(1));
String rusultStr = JsonHelper.getString(json, "result");
Identifier resultId = new Identifier(rusultStr);
ItemStack output = new ItemStack(Registry.ITEM.getOrEmpty(resultId).orElseThrow(() -> {
return new IllegalStateException("Item: " + rusultStr + " does not exist");
}));
float experience = JsonHelper.getFloat(json, "experience", 0.0F);
int smeltTime = JsonHelper.getInt(json, "smelttime", 350);
return new AlloyingRecipe(id, primaryInput, secondaryInput, output, experience, smeltTime);
}
@Override
public AlloyingRecipe read(Identifier id, PacketByteBuf packetBuffer) {
Ingredient primaryInput = Ingredient.fromPacket(packetBuffer);
Ingredient secondaryInput = Ingredient.fromPacket(packetBuffer);
ItemStack output = packetBuffer.readItemStack();
float experience = packetBuffer.readFloat();
int smeltTime = packetBuffer.readVarInt();
return new AlloyingRecipe(id, primaryInput, secondaryInput, output, experience, smeltTime);
}
@Override
public void write(PacketByteBuf packetBuffer, AlloyingRecipe recipe) {
recipe.primaryInput.write(packetBuffer);
recipe.secondaryInput.write(packetBuffer);
packetBuffer.writeItemStack(recipe.output);
packetBuffer.writeFloat(recipe.experience);
packetBuffer.writeInt(recipe.smeltTime);
}
}

View file

@ -7,34 +7,37 @@ import com.google.common.collect.Lists;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType; import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.block.entity.BlockEntityType.Builder;
import net.minecraft.item.BlockItem; import net.minecraft.item.BlockItem;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry; import net.minecraft.util.registry.Registry;
import ru.betterend.BetterEnd; import ru.betterend.BetterEnd;
import ru.betterend.blocks.EndStoneSmelter;
import ru.betterend.blocks.basis.BlockBarrel; import ru.betterend.blocks.basis.BlockBarrel;
import ru.betterend.blocks.basis.BlockChest; import ru.betterend.blocks.basis.BlockChest;
import ru.betterend.blocks.basis.BlockSign; import ru.betterend.blocks.basis.BlockSign;
import ru.betterend.blocks.entities.EBarrelBlockEntity; import ru.betterend.blocks.entities.EBarrelBlockEntity;
import ru.betterend.blocks.entities.EChestBlockEntity; import ru.betterend.blocks.entities.EChestBlockEntity;
import ru.betterend.blocks.entities.ESignBlockEntity; import ru.betterend.blocks.entities.ESignBlockEntity;
import ru.betterend.blocks.entities.EndStoneSmelterBlockEntity;
public class BlockEntityRegistry public class BlockEntityRegistry {
{ public final static BlockEntityType<EndStoneSmelterBlockEntity> END_STONE_SMELTER = registerBlockEntity(EndStoneSmelter.ID,
public static final BlockEntityType<EChestBlockEntity> CHEST = BlockEntityType.Builder.create(EChestBlockEntity::new, getChests()).build(null); BlockEntityType.Builder.create(EndStoneSmelterBlockEntity::new, BlockRegistry.END_STONE_SMELTER));
public static final BlockEntityType<EBarrelBlockEntity> BARREL = BlockEntityType.Builder.create(EBarrelBlockEntity::new, getBarrels()).build(null); public static final BlockEntityType<EChestBlockEntity> CHEST = registerBlockEntity("chest",
public static final BlockEntityType<ESignBlockEntity> SIGN = BlockEntityType.Builder.create(ESignBlockEntity::new, getSigns()).build(null); BlockEntityType.Builder.create(EChestBlockEntity::new, getChests()));
public static final BlockEntityType<EBarrelBlockEntity> BARREL = registerBlockEntity("barrel",
BlockEntityType.Builder.create(EBarrelBlockEntity::new, getBarrels()));
public static final BlockEntityType<ESignBlockEntity> SIGN = registerBlockEntity("sign",
BlockEntityType.Builder.create(ESignBlockEntity::new, getSigns()));
public static void register() { public static <T extends BlockEntity> BlockEntityType<T> registerBlockEntity(String id, BlockEntityType.Builder<T> builder) {
RegisterBlockEntity("chest", CHEST); return Registry.register(Registry.BLOCK_ENTITY_TYPE, new Identifier(BetterEnd.MOD_ID, id), builder.build(null));
RegisterBlockEntity("barrel", BARREL);
RegisterBlockEntity("sign", SIGN);
} }
public static void RegisterBlockEntity(String name, BlockEntityType<? extends BlockEntity> type) { public static void register() {}
Registry.register(Registry.BLOCK_ENTITY_TYPE, new Identifier(BetterEnd.MOD_ID, name), type);
}
private static Block[] getChests() { static Block[] getChests() {
List<Block> result = Lists.newArrayList(); List<Block> result = Lists.newArrayList();
ItemRegistry.getModBlocks().forEach((item) -> { ItemRegistry.getModBlocks().forEach((item) -> {
if (item instanceof BlockItem) { if (item instanceof BlockItem) {
@ -47,8 +50,7 @@ public class BlockEntityRegistry
return result.toArray(new Block[] {}); return result.toArray(new Block[] {});
} }
private static Block[] getBarrels() static Block[] getBarrels() {
{
List<Block> result = Lists.newArrayList(); List<Block> result = Lists.newArrayList();
ItemRegistry.getModBlocks().forEach((item) -> { ItemRegistry.getModBlocks().forEach((item) -> {
if (item instanceof BlockItem) { if (item instanceof BlockItem) {
@ -61,8 +63,7 @@ public class BlockEntityRegistry
return result.toArray(new Block[] {}); return result.toArray(new Block[] {});
} }
private static Block[] getSigns() static Block[] getSigns() {
{
List<Block> result = Lists.newArrayList(); List<Block> result = Lists.newArrayList();
ItemRegistry.getModBlocks().forEach((item) -> { ItemRegistry.getModBlocks().forEach((item) -> {
if (item instanceof BlockItem) { if (item instanceof BlockItem) {

View file

@ -46,8 +46,8 @@ public final class Logger {
this.log(Level.INFO, message, params); this.log(Level.INFO, message, params);
} }
public void warning(String message) { public void warning(String message, Object... params) {
this.log(Level.WARN, message); this.log(Level.WARN, message, params);
} }
public void warning(String message, Object obj, Exception ex) { public void warning(String message, Object obj, Exception ex) {