/
BlockTradein.java
193 lines (160 loc) · 8.91 KB
/
BlockTradein.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package beardlessbrady.modcurrency.block.tradein;
import beardlessbrady.modcurrency.block.EconomyBlockBase;
import beardlessbrady.modcurrency.block.TileEconomyBase;
import beardlessbrady.modcurrency.handler.StateHandler;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.EnumDyeColor;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
/**
* This class was created by BeardlessBrady. It is distributed as
* part of The Currency-Mod. Source Code located on github:
* https://github.com/BeardlessBrady/Currency-Mod
* -
* Copyright (C) All Rights Reserved
* File Created 2019-07-09
*/
public class BlockTradein extends EconomyBlockBase {
public BlockTradein() {
super("blocktradein", TileTradein.class);
}
/** Method activated when the block is RIGHT CLICKED */
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
TileTradein te = (TileTradein) getTile(worldIn, pos);
if (TileEconomyBase.EMPTYID.equals(getTile(worldIn, pos).getPlayerUsing())) { // Compares stored PLAYERUSING with an empty playerID to see if a player currently has the machine opened */
if(playerIn.getHeldItemMainhand().getItem() == Items.DYE){ // Checks if player is holding dye, if so color machine instead of opening*/
if(!playerIn.isCreative())
playerIn.getHeldItemMainhand().shrink(1);
te.setColor(EnumDyeColor.byDyeDamage(playerIn.getHeldItemMainhand().getItemDamage()));
// Code below used to update the block to force it realize it needs to change colour */
worldIn.markBlockRangeForRenderUpdate(pos, pos);
worldIn.notifyBlockUpdate(pos, worldIn.getBlockState(pos), worldIn.getBlockState(pos), 3);
worldIn.scheduleBlockUpdate(pos, this,0,0);
te.markDirty();
} else { // If player not holding dye, activate as normal */
if (playerIn.isSneaking() && (te.getOwner().equals(playerIn.getUniqueID()) || playerIn.isCreative())) { // Owning/Creative & Sneaking machine will open in STOCK MODE */
te.setField(TileEconomyBase.FIELD_MODE, 1);
} else { // Opens machine in TRADE MODE */
te.setField(TileEconomyBase.FIELD_MODE, 0);
}
playerIn.playSound(SoundEvents.BLOCK_IRON_TRAPDOOR_OPEN,0.2F, -100.0F);
if (!worldIn.isRemote) { // If CLIENT open GUI */
((TileTradein) getTile(worldIn, pos)).openGui(playerIn, worldIn, pos);
return true;
}
}
}
return true;
}
/** Method activated when block is placed **/
@Override
public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
worldIn.setBlockState(pos, state.withProperty(StateHandler.TWOTALL, StateHandler.EnumTwoBlock.TWOBOTTOM) // Places the 'bottom' part of the block since it is a 2 block high entity*/
.withProperty(StateHandler.FACING, placer.getHorizontalFacing().getOpposite()));
worldIn.setBlockState(pos.up(), state.withProperty(StateHandler.TWOTALL, StateHandler.EnumTwoBlock.TWOTOP) // Places the 'top' part of the block */
.withProperty(StateHandler.FACING, placer.getHorizontalFacing().getOpposite()));
getTile(worldIn, pos).setOwner(placer.getUniqueID()); // Sets owner */
getTile(worldIn, pos).markDirty();
}
/** Method activated when block is broken **/
@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state) {
if(!worldIn.isRemote) { // if CLIENT */
if (state.getValue(StateHandler.TWOTALL) == StateHandler.EnumTwoBlock.TWOTOP) { // If block activated is 2 high & block broken is 'top' break block under it as well */
worldIn.setBlockToAir(pos.down());
} else if (state.getValue(StateHandler.TWOTALL) == StateHandler.EnumTwoBlock.TWOBOTTOM) { // If block activated is 2 high & block broken is 'bottom' break block above it as well */
TileTradein te = (TileTradein) getTile(worldIn, pos, state);
//Outputs the change in STOCK then TRADE MODE
te.outChange(true);
te.dropInventory();
worldIn.setBlockToAir(pos.up());
}
super.breakBlock(worldIn, pos, state);
}
}
/** Getter method for the blocks tile**/
@Override
public TileEconomyBase getTile(World world, BlockPos pos) {
if (world.getBlockState(pos).getValue(StateHandler.TWOTALL) == StateHandler.EnumTwoBlock.TWOTOP){ // If 'top' part of block open tile from bottom under it since the tile is stored in the bottom block*/
if (world.getTileEntity(pos.down()) instanceof TileTradein)
return (TileTradein) world.getTileEntity(pos.down());
}else{ // If 'bottom' part of block open tile normally since the tile is stored in the bottom block*/
if (world.getTileEntity(pos) instanceof TileTradein)
return (TileTradein) world.getTileEntity(pos);
}
return null;
}
/** Getter method for the blocks tile**/
public TileEconomyBase getTile(World world, BlockPos pos, IBlockState state) {
if (state.getValue(StateHandler.TWOTALL) == StateHandler.EnumTwoBlock.TWOTOP){ // If 'top' part of block open tile from bottom under it since the tile is stored in the bottom block*/
if (world.getTileEntity(pos.down()) instanceof TileTradein)
return (TileTradein) world.getTileEntity(pos.down());
} else { // If 'bottom' part of block open tile normally since the tile is stored in the bottom block*/
if (world.getTileEntity(pos) instanceof TileTradein)
return (TileTradein) world.getTileEntity(pos);
}
return null;
}
/** Getter method for the blocks tile**/
public TileEconomyBase getTile(IBlockAccess world, BlockPos pos, IBlockState state) {
if (state.getValue(StateHandler.TWOTALL) == StateHandler.EnumTwoBlock.TWOTOP){
if (world.getTileEntity(pos.down()) instanceof TileTradein)
return (TileTradein) world.getTileEntity(pos.down());
}else{
if (world.getTileEntity(pos) instanceof TileTradein)
return (TileTradein) world.getTileEntity(pos);
}
return null;
}
/** Block State Methods **/
//<editor-fold desc="Block State Methods">
@Override
protected BlockStateContainer createBlockState() {
return new BlockStateContainer(this, StateHandler.FACING, StateHandler.TWOTALL);
}
@Override
public IBlockState getStateFromMeta(int meta) {
return this.getDefaultState().withProperty(StateHandler.FACING, EnumFacing.getHorizontal(meta % 4))
.withProperty(StateHandler.TWOTALL, StateHandler.EnumTwoBlock.class.getEnumConstants()[meta / 4]);
}
@Override
public int getMetaFromState(IBlockState state) {
return (state.getValue(StateHandler.FACING).getHorizontalIndex() + (state.getValue(StateHandler.TWOTALL).ordinal() * 4));
}
@Override
public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos) {
TileTradein tile;
if (StateHandler.EnumTwoBlock.class.getEnumConstants()[getMetaFromState(state) / 4] == StateHandler.EnumTwoBlock.TWOTOP) {
tile = (TileTradein) worldIn.getTileEntity(pos.down());
} else {
tile = (TileTradein) worldIn.getTileEntity(pos);
}
return this.getDefaultState().withProperty(StateHandler.FACING, EnumFacing.getHorizontal(getMetaFromState(state) % 4))
.withProperty(StateHandler.TWOTALL, StateHandler.EnumTwoBlock.class.getEnumConstants()[getMetaFromState(state) / 4]);
}
//</editor-fold>
/** Rendering Methods **/
//<editor-fold desc="Rendering Methods">
@Override
@SideOnly(Side.CLIENT)
public void registerModel() {
super.registerModel();
ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(this), 0, new ModelResourceLocation(getRegistryName(), "inventory"));
}
//</editor-fold>
}