This repository has been archived by the owner on Jul 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ItemComputerUpgrade.java
195 lines (164 loc) · 6.69 KB
/
ItemComputerUpgrade.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
194
195
package org.squiddev.cctweaks.items;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.shared.computer.blocks.BlockComputer;
import dan200.computercraft.shared.computer.blocks.TileComputerBase;
import dan200.computercraft.shared.computer.core.ComputerFamily;
import dan200.computercraft.shared.computer.items.ComputerItemFactory;
import dan200.computercraft.shared.pocket.items.PocketComputerItemFactory;
import dan200.computercraft.shared.turtle.blocks.TileTurtle;
import dan200.computercraft.shared.turtle.items.TurtleItemFactory;
import dan200.computercraft.shared.util.ImpostorRecipe;
import dan200.computercraft.shared.util.ImpostorShapelessRecipe;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.Style;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.oredict.RecipeSorter;
import org.squiddev.cctweaks.CCTweaks;
import org.squiddev.cctweaks.core.Config;
import org.squiddev.cctweaks.core.utils.ComputerAccessor;
import org.squiddev.cctweaks.core.utils.DebugLogger;
import org.squiddev.cctweaks.core.utils.Helpers;
import org.squiddev.cctweaks.core.utils.InventoryUtils;
import javax.annotation.Nonnull;
import java.util.List;
public class ItemComputerUpgrade extends ItemComputerAction {
public ItemComputerUpgrade() {
super("computerUpgrade");
}
@Nonnull
@Override
public EnumActionResult onItemUse(ItemStack stack, EntityPlayer player, World world, BlockPos position, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
if (Config.Computer.computerUpgradeEnabled) {
return super.onItemUse(stack, player, world, position, hand, side, hitX, hitY, hitZ);
} else {
return EnumActionResult.PASS;
}
}
@Override
protected boolean useComputer(ItemStack stack, EntityPlayer player, TileComputerBase computerTile, EnumFacing side) {
net.minecraft.util.math.BlockPos position = computerTile.getPos();
World world = computerTile.getWorld();
// Check we can copy the tile and it is a normal computer
if (computerTile.getFamily() != ComputerFamily.Normal) {
return false;
}
// Set metadata
IBlockState state = world.getBlockState(position);
if (state.getValue(BlockComputer.Properties.ADVANCED)) {
return false;
}
// Ensure we can break/place this block
if (!world.isBlockModifiable(player, computerTile.getPos())) {
return false;
}
world.setBlockState(position, state.withProperty(BlockComputer.Properties.ADVANCED, true));
computerTile.invalidate();
computerTile.validate();
return true;
}
@Override
protected boolean useTurtle(ItemStack stack, EntityPlayer player, TileTurtle computerTile, EnumFacing side) {
BlockPos position = computerTile.getPos();
World world = computerTile.getWorld();
// Ensure it is a normal computer
if (computerTile.getFamily() != ComputerFamily.Normal) {
return false;
}
if (!player.capabilities.isCreativeMode) {
int remaining = InventoryUtils.extractItems(player.inventory, Items.GOLD_INGOT, 7);
if (remaining > 0) {
player.addChatMessage(new TextComponentString("7 gold required. Need " + remaining + " more.").setStyle(new Style().setColor(TextFormatting.DARK_RED)));
return false;
}
player.inventoryContainer.detectAndSendChanges();
}
// Ensure we can break/place this block
if (!world.isBlockModifiable(player, computerTile.getPos())) {
return false;
}
// If we set the turtle as moved, the destroy method won't drop the items
try {
ComputerAccessor.tileTurtleMoveState.set(computerTile, ComputerAccessor.tileTurtleMoveStateMoved);
} catch (Exception e) {
DebugLogger.warn("Cannot set TurtleTile m_moved in ItemComputerUpgrade", e);
return false;
}
// Set block as AdvancedTurtle
world.setBlockState(position, ComputerCraft.Blocks.turtleAdvanced.getDefaultState());
TileEntity newTile = world.getTileEntity(position);
// Transfer state
if (newTile == null || !(newTile instanceof TileTurtle)) {
return false;
}
TileTurtle newTurtle = (TileTurtle) newTile;
newTurtle.transferStateFrom(computerTile);
// Invalidate & revalidate to unload the instance ID
newTurtle.invalidate();
newTurtle.validate();
// And update the computer
newTurtle.createServerComputer().setWorld(world);
newTurtle.createServerComputer().setPosition(position);
newTurtle.updateBlock();
return true;
}
@Override
@SideOnly(Side.CLIENT)
@SuppressWarnings({"unchecked", "rawtypes"})
public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean bool) {
list.add(Helpers.translateToLocal("gui.tooltip.cctweaks.computerUpgrade.normal"));
}
@Override
public void init() {
super.init();
if (!Config.Computer.computerUpgradeEnabled) return;
ItemStack stack = new ItemStack(this);
if (Config.Computer.computerUpgradeCrafting) {
GameRegistry.addRecipe(stack, "GGG", "GSG", "GSG", 'G', Items.GOLD_INGOT, 'S', Blocks.STONE);
}
RecipeSorter.register(CCTweaks.ID + ":computer_upgrade_crafting", CraftingComputerUpgrade.class, RecipeSorter.Category.SHAPELESS, "after:minecraft:shapeless");
GameRegistry.addRecipe(new CraftingComputerUpgrade());
// Add some impostor recipes for NEI. We just use CC's default ones
{
// Computer
GameRegistry.addRecipe(new ImpostorShapelessRecipe(
ComputerItemFactory.create(-1, null, ComputerFamily.Advanced),
new ItemStack[]{
ComputerItemFactory.create(-1, null, ComputerFamily.Normal),
stack
}
));
// Turtle (Is is silly to include every possible upgrade so we just do the normal one)
ItemStack gold = new ItemStack(Items.GOLD_INGOT);
GameRegistry.addRecipe(new ImpostorRecipe(3, 3,
new ItemStack[]{
gold, gold, gold,
gold, TurtleItemFactory.create(-1, null, -1, ComputerFamily.Normal, null, null, 0, null), gold,
gold, stack, gold,
},
TurtleItemFactory.create(-1, null, -1, ComputerFamily.Advanced, null, null, 0, null)
));
// Pocket computer
GameRegistry.addRecipe(new ImpostorShapelessRecipe(
PocketComputerItemFactory.create(-1, null, -1, ComputerFamily.Advanced, null),
new ItemStack[]{
PocketComputerItemFactory.create(-1, null, -1, ComputerFamily.Normal, null),
stack
}
));
}
}
}