Add bookshelves

This commit is contained in:
Electroblob77
2020-03-25 14:29:27 +00:00
parent d9c3d15b9b
commit 004c4c09e3
89 changed files with 1572 additions and 314 deletions
@@ -14,7 +14,7 @@ import electroblob.wizardry.packet.PacketControlInput;
import electroblob.wizardry.packet.WizardryPacketHandler;
import electroblob.wizardry.registry.WizardrySounds;
import electroblob.wizardry.spell.Spell;
import electroblob.wizardry.tileentity.ContainerArcaneWorkbench;
import electroblob.wizardry.inventory.ContainerArcaneWorkbench;
import electroblob.wizardry.tileentity.TileEntityArcaneWorkbench;
import electroblob.wizardry.util.WandHelper;
import net.minecraft.client.Minecraft;
@@ -0,0 +1,54 @@
package electroblob.wizardry.client.gui;
import electroblob.wizardry.Wizardry;
import electroblob.wizardry.inventory.ContainerBookshelf;
import electroblob.wizardry.tileentity.TileEntityBookshelf;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class GuiBookshelf extends GuiContainer {
private static final ResourceLocation TEXTURE = new ResourceLocation(Wizardry.MODID, "textures/gui/bookshelf.png");
/** The player inventory bound to this GUI. */
private final InventoryPlayer playerInventory;
/** The inventory contained within the corresponding bookshelf. */
public IInventory bookshelfInventory;
public GuiBookshelf(InventoryPlayer playerInv, TileEntityBookshelf bookshelfInv){
super(new ContainerBookshelf(playerInv, bookshelfInv));
this.playerInventory = playerInv;
this.bookshelfInventory = bookshelfInv;
this.allowUserInput = false;
this.ySize = 148;
}
@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks){
this.drawDefaultBackground();
super.drawScreen(mouseX, mouseY, partialTicks);
this.renderHoveredToolTip(mouseX, mouseY);
}
@Override
protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY){
String s = this.bookshelfInventory.getDisplayName().getUnformattedText();
this.fontRenderer.drawString(s, 8, 6, 4210752);
this.fontRenderer.drawString(this.playerInventory.getDisplayName().getUnformattedText(), 8, this.ySize - 96 + 2, 4210752);
}
@Override
protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY){
GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
this.mc.getTextureManager().bindTexture(TEXTURE);
int i = (this.width - this.xSize) / 2;
int j = (this.height - this.ySize) / 2;
this.drawTexturedModalRect(i, j, 0, 0, this.xSize, this.ySize);
}
}
@@ -0,0 +1,72 @@
package electroblob.wizardry.client.model;
import electroblob.wizardry.block.BlockBookshelf;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.block.model.IBakedModel;
import net.minecraft.client.renderer.block.model.ItemOverrideList;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.property.IExtendedBlockState;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
public class BakedModelBookshelf implements IBakedModel {
private final IBakedModel bookshelf;
private final IBakedModel[] books;
public BakedModelBookshelf(IBakedModel bookshelf, IBakedModel... books){
this.bookshelf = bookshelf;
this.books = books;
}
@Override
public List<BakedQuad> getQuads(@Nullable IBlockState state, @Nullable EnumFacing side, long rand){
IBakedModel fallback = Minecraft.getMinecraft().getBlockRendererDispatcher().getBlockModelShapes().getModelManager().getMissingModel();
if(state == null) return fallback.getQuads(null, side, rand);
// It took me waaay too long to realise I needed a new list here
List<BakedQuad> quads = new ArrayList<>(bookshelf.getQuads(state, side, rand)); // Lists ain't immutable, chief
IExtendedBlockState extendedState = (IExtendedBlockState)state;
for(int i = 0; i<BlockBookshelf.SLOT_COUNT; i++){
if(extendedState.getValue(BlockBookshelf.BOOKS[i])) quads.addAll(books[i].getQuads(state, side, rand));
}
return quads;
}
@Override
public boolean isBuiltInRenderer(){
return false;
}
// Delegate everything else to the main bookshelf model
@Override
public boolean isAmbientOcclusion(){
return bookshelf.isAmbientOcclusion();
}
@Override
public boolean isGui3d(){
return bookshelf.isGui3d();
}
@Override
public TextureAtlasSprite getParticleTexture(){
return bookshelf.getParticleTexture(); // TESTME: May need to do this manually
}
@Override
public ItemOverrideList getOverrides(){
return bookshelf.getOverrides(); // TESTME: Same here
}
}
@@ -0,0 +1,53 @@
package electroblob.wizardry.client.model;
import electroblob.wizardry.Wizardry;
import electroblob.wizardry.block.BlockBookshelf;
import net.minecraft.client.renderer.block.model.IBakedModel;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.vertex.VertexFormat;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.model.IModel;
import net.minecraftforge.client.model.ModelLoaderRegistry;
import net.minecraftforge.common.model.IModelState;
import java.util.function.Function;
public class ModelBookshelf implements IModel {
private final ModelResourceLocation bookshelfModelLocation;
private final String variant;
public ModelBookshelf(String bookshelfModelName){
String[] args = bookshelfModelName.split("#", 2);
String model = args[0];
variant = args.length == 2 ? args[1] : null;
bookshelfModelLocation = new ModelResourceLocation(new ResourceLocation(Wizardry.MODID, "bookshelf_parts/" + model), variant);
}
@Override
public IBakedModel bake(IModelState state, VertexFormat format, Function<ResourceLocation, TextureAtlasSprite> bakedTextureGetter){
try {
IModel bookshelfModel = ModelLoaderRegistry.getModel(bookshelfModelLocation);
// I don't know why default state works here (surely it ought to be the state param?), but it works so who cares
IBakedModel bookshelf = bookshelfModel.bake(bookshelfModel.getDefaultState(), format, bakedTextureGetter);
IBakedModel[] books = new IBakedModel[BlockBookshelf.SLOT_COUNT];
for(int i = 0; i < BlockBookshelf.SLOT_COUNT; i++){
IModel bookModel = ModelLoaderRegistry.getModel(new ModelResourceLocation(new ResourceLocation(Wizardry.MODID, "bookshelf_parts/books" + i), variant));
books[i] = bookModel.bake(bookModel.getDefaultState(), format, bakedTextureGetter); // Same here!
}
return new BakedModelBookshelf(bookshelf, books);
}catch(Exception exception){
Wizardry.logger.error("Error baking bookshelf models: ", exception);
return ModelLoaderRegistry.getMissingModel().bake(state, format, bakedTextureGetter);
}
}
}
@@ -0,0 +1,28 @@
package electroblob.wizardry.client.model;
import electroblob.wizardry.Wizardry;
import net.minecraft.client.resources.IResourceManager;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.model.ICustomModelLoader;
import net.minecraftforge.client.model.IModel;
public class ModelLoaderBookshelf implements ICustomModelLoader {
private static final String SMART_MODEL_PREFIX = "models/block/smart_model/";
@Override
public void onResourceManagerReload(IResourceManager resourceManager){
// Do nothing
}
@Override
public boolean accepts(ResourceLocation modelLocation){
return modelLocation.getNamespace().equals(Wizardry.MODID) && modelLocation.getPath().startsWith(SMART_MODEL_PREFIX);
}
@Override
public IModel loadModel(ResourceLocation modelLocation){
return new ModelBookshelf(modelLocation.getPath().substring(SMART_MODEL_PREFIX.length()));
}
}
@@ -21,6 +21,7 @@ import net.minecraft.util.NonNullList;
import net.minecraftforge.client.event.ModelBakeEvent;
import net.minecraftforge.client.event.ModelRegistryEvent;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.client.model.ModelLoaderRegistry;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
@@ -69,6 +70,16 @@ public final class WizardryModels {
ItemBlockMultiTextured gildedWoodItem = (ItemBlockMultiTextured)Item.getItemFromBlock(WizardryBlocks.gilded_wood);
registerMultiTexturedModel(gildedWoodItem);
// Explanation for all this here -> https://github.com/TheGreyGhost/MinecraftByExample/tree/master/src/main/java/minecraftbyexample/mbe05_block_dynamic_block_model2
ModelLoaderRegistry.registerLoader(new ModelLoaderBookshelf());
registerItemModel(Item.getItemFromBlock(WizardryBlocks.oak_bookshelf));
registerItemModel(Item.getItemFromBlock(WizardryBlocks.spruce_bookshelf));
registerItemModel(Item.getItemFromBlock(WizardryBlocks.birch_bookshelf));
registerItemModel(Item.getItemFromBlock(WizardryBlocks.jungle_bookshelf));
registerItemModel(Item.getItemFromBlock(WizardryBlocks.acacia_bookshelf));
registerItemModel(Item.getItemFromBlock(WizardryBlocks.dark_oak_bookshelf));
// Items
registerMultiTexturedModel((ItemCrystal)WizardryItems.magic_crystal);
@@ -1,7 +1,7 @@
package electroblob.wizardry.client.renderer;
import electroblob.wizardry.Wizardry;
import electroblob.wizardry.tileentity.ContainerArcaneWorkbench;
import electroblob.wizardry.inventory.ContainerArcaneWorkbench;
import electroblob.wizardry.tileentity.TileEntityArcaneWorkbench;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.BufferBuilder;