Add receptacles

This commit is contained in:
Electroblob77
2020-05-11 18:42:00 +01:00
parent 11821ff14d
commit a97abd26d0
15 changed files with 803 additions and 6 deletions
@@ -0,0 +1,211 @@
package electroblob.wizardry.block;
import com.google.common.collect.Maps;
import electroblob.wizardry.constants.Element;
import electroblob.wizardry.item.ItemSpectralDust;
import electroblob.wizardry.registry.WizardryItems;
import electroblob.wizardry.registry.WizardrySounds;
import electroblob.wizardry.registry.WizardryTabs;
import electroblob.wizardry.tileentity.TileEntityReceptacle;
import electroblob.wizardry.util.ParticleBuilder;
import electroblob.wizardry.util.WizardryUtilities;
import net.minecraft.block.BlockTorch;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.SoundType;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.NonNullList;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import javax.annotation.Nullable;
import java.util.Map;
import java.util.Random;
public class BlockReceptacle extends BlockTorch implements ITileEntityProvider {
protected static final AxisAlignedBB STANDING_AABB = new AxisAlignedBB(4/16d, 0/16d, 4/16d, 12/16d, 8/16d, 12/16d);
protected static final AxisAlignedBB NORTH_WALL_AABB = new AxisAlignedBB(4/16d, 2/16d, 7/16d, 12/16d, 10/16d, 16/16d);
protected static final AxisAlignedBB SOUTH_WALL_AABB = new AxisAlignedBB(4/16d, 2/16d, 0/16d, 12/16d, 10/16d, 9/16d);
protected static final AxisAlignedBB WEST_WALL_AABB = new AxisAlignedBB(7/16d, 2/16d, 4/16d, 16/16d, 10/16d, 12/16d);
protected static final AxisAlignedBB EAST_WALL_AABB = new AxisAlignedBB(0/16d, 2/16d, 4/16d, 9/16d, 10/16d, 12/16d);
private static final double WALL_PARTICLE_OFFSET = 3/16d;
private static final Map<Element, int[]> PARTICLE_COLOURS;
static {
Map<Element, int[]> map = Maps.newEnumMap(Element.class);
map.put(Element.MAGIC, new int[]{0xe4c7cd, 0xfeffbe, 0x9d2cf3});
map.put(Element.FIRE, new int[]{0xff9600, 0xfffe67, 0xd02700});
map.put(Element.ICE, new int[]{0xa3e8f4, 0xe9f9fc, 0x138397});
map.put(Element.LIGHTNING, new int[]{0x409ee1, 0xf5f0ff, 0x225474});
map.put(Element.NECROMANCY, new int[]{0xa811ce, 0xf575f5, 0x382366});
map.put(Element.EARTH, new int[]{0xa8f408, 0xc8ffb2, 0x795c28});
map.put(Element.SORCERY, new int[]{0x56e8e3, 0xe8fcfc, 0x16a64d});
map.put(Element.HEALING, new int[]{0xfff69e, 0xfffff6, 0xa18200});
PARTICLE_COLOURS = Maps.immutableEnumMap(map);
}
public BlockReceptacle(){
super();
this.setCreativeTab(WizardryTabs.WIZARDRY);
this.setHardness(0);
this.setLightLevel(0.5f);
this.setSoundType(SoundType.STONE);
}
@Override
public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess world, BlockPos pos){
switch(state.getValue(FACING)){
case EAST: return EAST_WALL_AABB;
case WEST: return WEST_WALL_AABB;
case SOUTH: return SOUTH_WALL_AABB;
case NORTH: return NORTH_WALL_AABB;
default: return STANDING_AABB;
}
}
@Override
public AxisAlignedBB getCollisionBoundingBox(IBlockState state, IBlockAccess world, BlockPos pos){
return state.getBoundingBox(world, pos);
}
@Override
public int getLightValue(IBlockState state, IBlockAccess world, BlockPos pos){
TileEntity tileEntity = world.getTileEntity(pos);
if(tileEntity instanceof TileEntityReceptacle && ((TileEntityReceptacle)tileEntity).getElement() != null){
return super.getLightValue(state, world, pos); // Return super to use float value from constructor
}
return 0;
}
@Override
public void getDrops(NonNullList<ItemStack> drops, IBlockAccess world, BlockPos pos, IBlockState state, int fortune){
super.getDrops(drops, world, pos, state, fortune);
TileEntity tileEntity = world.getTileEntity(pos);
if(tileEntity instanceof TileEntityReceptacle){
Element element = ((TileEntityReceptacle)tileEntity).getElement();
if(element != null) drops.add(new ItemStack(WizardryItems.spectral_dust, 1, element.ordinal()));
}
}
// See BlockFlowerPot for these two (this class is essentially based on a flower pot)
@Override
public boolean removedByPlayer(IBlockState state, World world, BlockPos pos, EntityPlayer player, boolean willHarvest){
if(willHarvest) return true; // If it will harvest, delay deletion of the block until after getDrops
return super.removedByPlayer(state, world, pos, player, willHarvest);
}
@Override
public void harvestBlock(World world, EntityPlayer player, BlockPos pos, IBlockState state, @Nullable TileEntity te, ItemStack tool){
super.harvestBlock(world, player, pos, state, te, tool);
world.setBlockToAir(pos);
}
@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ){
TileEntity tileEntity = world.getTileEntity(pos);
ItemStack stack = player.getHeldItem(hand);
if(tileEntity instanceof TileEntityReceptacle){
Element currentElement = ((TileEntityReceptacle)tileEntity).getElement();
if(currentElement == null){
if(stack.getItem() instanceof ItemSpectralDust && stack.getMetadata() >= 0
&& stack.getMetadata() < Element.values().length){
((TileEntityReceptacle)tileEntity).setElement(Element.values()[stack.getMetadata()]);
if(!player.capabilities.isCreativeMode) stack.shrink(1);
world.playSound(pos.getX(), pos.getY(), pos.getZ(), WizardrySounds.BLOCK_RECEPTACLE_IGNITE,
SoundCategory.BLOCKS, 0.7f, 0.7f, false);
world.checkLight(pos);
return true;
}
}else{
((TileEntityReceptacle)tileEntity).setElement(null);
ItemStack dust = new ItemStack(WizardryItems.spectral_dust, 1, currentElement.ordinal());
if(stack.isEmpty()){
player.setHeldItem(hand, dust);
}else if(!player.addItemStackToInventory(dust)){
player.dropItem(dust, false);
}
world.checkLight(pos);
return true;
}
}
return super.onBlockActivated(world, pos, state, player, hand, facing, hitX, hitY, hitZ);
}
@Override
public void randomDisplayTick(IBlockState state, World world, BlockPos pos, Random rand){
TileEntity tileEntity = world.getTileEntity(pos);
if(tileEntity instanceof TileEntityReceptacle){
Element element = ((TileEntityReceptacle)tileEntity).getElement();
if(element != null){
EnumFacing facing = state.getValue(FACING).getOpposite();
Vec3d centre = WizardryUtilities.getCentre(pos);
if(facing.getAxis().isHorizontal()){
centre = centre.add(new Vec3d(facing.getDirectionVec()).scale(WALL_PARTICLE_OFFSET)).add(0, 0.125, 0);
}
int[] colours = PARTICLE_COLOURS.get(element);
ParticleBuilder.create(ParticleBuilder.Type.FLASH).pos(centre).scale(0.35f).time(48).clr(colours[0]).spawn(world);
double r = 0.12;
for(int i = 0; i < 3; i++){
double x = r * (rand.nextDouble() * 2 - 1);
double y = r * (rand.nextDouble() * 2 - 1);
double z = r * (rand.nextDouble() * 2 - 1);
ParticleBuilder.create(ParticleBuilder.Type.DUST).pos(centre.x + x, centre.y + y, centre.z + z)
.vel(x * -0.03, 0.02, z * -0.03).time(24 + rand.nextInt(8)).clr(colours[1]).fade(colours[2]).spawn(world);
}
}
}
}
@Nullable
@Override
public TileEntity createNewTileEntity(World world, int meta){
return new TileEntityReceptacle();
}
}
@@ -7,23 +7,20 @@ import electroblob.wizardry.block.BlockRunestone;
import electroblob.wizardry.item.IMultiTexturedItem;
import electroblob.wizardry.item.ItemBlockMultiTextured;
import electroblob.wizardry.item.ItemCrystal;
import electroblob.wizardry.item.ItemSpectralDust;
import electroblob.wizardry.registry.WizardryBlocks;
import electroblob.wizardry.registry.WizardryItems;
import net.minecraft.block.BlockPlanks;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.block.model.IBakedModel;
import net.minecraft.client.renderer.block.model.ModelBakery;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.client.renderer.block.statemap.StateMap;
import net.minecraft.client.renderer.block.statemap.StateMapperBase;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.event.ModelBakeEvent;
import net.minecraftforge.client.event.ModelRegistryEvent;
import net.minecraftforge.client.event.TextureStitchEvent;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.client.model.ModelLoaderRegistry;
import net.minecraftforge.fml.common.Mod;
@@ -91,6 +88,8 @@ public final class WizardryModels {
registerItemModel(Item.getItemFromBlock(WizardryBlocks.acacia_lectern));
registerItemModel(Item.getItemFromBlock(WizardryBlocks.dark_oak_lectern));
registerItemModel(Item.getItemFromBlock(WizardryBlocks.receptacle));
// Items
registerMultiTexturedModel((ItemCrystal)WizardryItems.magic_crystal);
@@ -179,6 +178,8 @@ public final class WizardryModels {
registerItemModel(WizardryItems.magic_silk);
registerMultiTexturedModel((ItemSpectralDust)WizardryItems.spectral_dust);
registerItemModel(WizardryItems.wizard_hat);
registerItemModel(WizardryItems.wizard_robe);
registerItemModel(WizardryItems.wizard_leggings);
@@ -4,8 +4,6 @@ import electroblob.wizardry.Wizardry;
import electroblob.wizardry.block.*;
import electroblob.wizardry.tileentity.*;
import net.minecraft.block.Block;
import net.minecraft.block.BlockPlanks;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.event.RegistryEvent;
@@ -77,6 +75,8 @@ public final class WizardryBlocks {
public static final Block acacia_lectern = placeholder();
public static final Block dark_oak_lectern = placeholder();
public static final Block receptacle = placeholder();
/**
* Sets both the registry and unlocalised names of the given block, then registers it with the given registry. Use
* this instead of {@link Block#setRegistryName(String)} and {@link Block#setTranslationKey(String)} during
@@ -134,6 +134,8 @@ public final class WizardryBlocks {
registerBlock(registry, "acacia_lectern", new BlockLectern());
registerBlock(registry, "dark_oak_lectern", new BlockLectern());
registerBlock(registry, "receptacle", new BlockReceptacle());
}
/** Called from the preInit method in the main mod class to register all the tile entities. */
@@ -148,5 +150,6 @@ public final class WizardryBlocks {
GameRegistry.registerTileEntity(TileEntityShrineCore.class, new ResourceLocation(Wizardry.MODID, "shrine_core"));
GameRegistry.registerTileEntity(TileEntityBookshelf.class, new ResourceLocation(Wizardry.MODID, "bookshelf"));
GameRegistry.registerTileEntity(TileEntityLectern.class, new ResourceLocation(Wizardry.MODID, "lectern"));
GameRegistry.registerTileEntity(TileEntityReceptacle.class, new ResourceLocation(Wizardry.MODID, "receptacle"));
}
}
@@ -416,6 +416,8 @@ public final class WizardryItems {
registerItemBlock(registry, WizardryBlocks.acacia_lectern);
registerItemBlock(registry, WizardryBlocks.dark_oak_lectern);
registerItemBlock(registry, WizardryBlocks.receptacle);
// Items
registerItem(registry, "magic_crystal", new ItemCrystal());
@@ -29,6 +29,7 @@ public final class WizardrySounds {
public static final SoundEvent BLOCK_PEDESTAL_ACTIVATE = createSound("block.pedestal.activate");
public static final SoundEvent BLOCK_PEDESTAL_CONQUER = createSound("block.pedestal.conquer");
public static final SoundEvent BLOCK_LECTERN_LOCATE_SPELL = createSound("block.lectern.locate_spell");
public static final SoundEvent BLOCK_RECEPTACLE_IGNITE = createSound("block.receptacle.ignite");
public static final SoundEvent ITEM_WAND_SWITCH_SPELL = createSound("item.wand.switch_spell");
public static final SoundEvent ITEM_WAND_LEVELUP = createSound("item.wand.levelup");
@@ -156,6 +157,7 @@ public final class WizardrySounds {
event.getRegistry().register(BLOCK_PEDESTAL_ACTIVATE);
event.getRegistry().register(BLOCK_PEDESTAL_CONQUER);
event.getRegistry().register(BLOCK_LECTERN_LOCATE_SPELL);
event.getRegistry().register(BLOCK_RECEPTACLE_IGNITE);
event.getRegistry().register(ITEM_WAND_SWITCH_SPELL);
event.getRegistry().register(ITEM_WAND_LEVELUP);
@@ -0,0 +1,61 @@
package electroblob.wizardry.tileentity;
import electroblob.wizardry.constants.Element;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import javax.annotation.Nullable;
public class TileEntityReceptacle extends TileEntity {
private Element element;
public TileEntityReceptacle(){
this(null);
}
public TileEntityReceptacle(Element element){
this.element = element;
}
public Element getElement(){
return element;
}
public void setElement(Element element){
this.element = element;
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound){
super.writeToNBT(compound);
compound.setInteger("Element", element == null ? -1 : element.ordinal());
return compound;
}
@Override
public void readFromNBT(NBTTagCompound compound){
super.readFromNBT(compound);
int i = compound.getInteger("Element");
element = i == -1 ? null : Element.values()[i];
}
@Override
public NBTTagCompound getUpdateTag(){
return this.writeToNBT(new NBTTagCompound());
}
@Nullable
@Override
public SPacketUpdateTileEntity getUpdatePacket(){
return new SPacketUpdateTileEntity(pos, getBlockMetadata(), this.getUpdateTag());
}
@Override
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt){
readFromNBT(pkt.getNbtCompound());
}
}