TechReborn/src/main/java/techreborn/items/ItemUpgrades.java

161 lines
5.3 KiB
Java
Raw Normal View History

/*
* This file is part of TechReborn, licensed under the MIT License (MIT).
*
2018-02-11 15:08:08 +01:00
* Copyright (c) 2018 TechReborn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
2015-06-09 12:24:15 +02:00
package techreborn.items;
import net.minecraft.client.Minecraft;
2017-06-11 22:22:07 +02:00
import net.minecraft.client.util.ITooltipFlag;
2015-06-09 12:24:15 +02:00
import net.minecraft.creativetab.CreativeTabs;
2016-03-29 09:30:05 +02:00
import net.minecraft.entity.player.EntityPlayer;
2017-04-13 12:26:16 +02:00
import net.minecraft.inventory.Container;
2015-06-09 12:24:15 +02:00
import net.minecraft.item.ItemStack;
2017-04-11 21:42:55 +02:00
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.NonNullList;
2017-06-11 22:22:07 +02:00
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
2017-04-11 01:30:27 +02:00
import reborncore.api.tile.IUpgrade;
2017-04-11 20:26:46 +02:00
import reborncore.common.powerSystem.TilePowerAcceptor;
import reborncore.common.recipes.IUpgradeHandler;
2017-04-11 01:30:27 +02:00
import reborncore.common.tile.TileLegacyMachineBase;
import reborncore.common.util.ItemNBTHelper;
import techreborn.Core;
2015-06-09 12:24:15 +02:00
import techreborn.client.TechRebornCreativeTabMisc;
import techreborn.client.container.builder.BuiltContainer;
import techreborn.client.container.builder.ContainerBuilder;
import techreborn.client.gui.upgrades.GuiSideConfig;
2015-06-09 12:24:15 +02:00
import techreborn.init.ModItems;
2017-04-11 01:30:27 +02:00
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.security.InvalidParameterException;
import java.util.List;
2015-06-09 12:24:15 +02:00
2017-06-14 01:49:52 +02:00
public class ItemUpgrades extends ItemTR implements IUpgrade {
public static final String[] types = new String[] { "overclock", "transformer", "energy_storage", "range" };
2016-10-08 21:46:16 +02:00
public ItemUpgrades() {
2016-03-25 10:47:34 +01:00
setUnlocalizedName("techreborn.upgrade");
setHasSubtypes(true);
setCreativeTab(TechRebornCreativeTabMisc.instance);
setMaxStackSize(1);
2016-03-25 10:47:34 +01:00
}
2016-10-08 21:46:16 +02:00
public static ItemStack getUpgradeByName(String name, int count) {
for (int i = 0; i < types.length; i++) {
if (types[i].equalsIgnoreCase(name)) {
return new ItemStack(ModItems.UPGRADES, count, i);
2016-03-25 10:47:34 +01:00
}
}
throw new InvalidParameterException("The upgrade " + name + " could not be found.");
}
2016-10-08 21:46:16 +02:00
public static ItemStack getUpgradeByName(String name) {
2016-03-25 10:47:34 +01:00
return getUpgradeByName(name, 1);
}
2016-03-25 10:47:34 +01:00
@Override
// gets Unlocalized Name depending on meta data
2016-10-08 21:46:16 +02:00
public String getUnlocalizedName(ItemStack itemStack) {
2016-03-25 10:47:34 +01:00
int meta = itemStack.getItemDamage();
2016-10-08 21:46:16 +02:00
if (meta < 0 || meta >= types.length) {
2016-03-25 10:47:34 +01:00
meta = 0;
}
2016-03-25 10:47:34 +01:00
return super.getUnlocalizedName() + "." + types[meta];
}
2016-03-25 10:47:34 +01:00
// Adds Dusts SubItems To Creative Tab
@Override
2017-09-14 16:55:28 +02:00
public void getSubItems(CreativeTabs creativeTabs, NonNullList<ItemStack> list) {
2017-06-28 21:54:12 +02:00
if (!isInCreativeTab(creativeTabs)) {
2017-06-12 18:30:21 +02:00
return;
}
2016-10-08 21:46:16 +02:00
for (int meta = 0; meta < types.length; ++meta) {
2017-06-11 22:22:07 +02:00
list.add(new ItemStack(this, 1, meta));
2016-03-25 10:47:34 +01:00
}
}
@Override
2017-06-11 22:22:07 +02:00
public void addInformation(ItemStack stack, World world, List<String> tooltip, ITooltipFlag flag) {
super.addInformation(stack, world, tooltip, flag);
2017-06-12 15:23:32 +02:00
if (stack.getItemDamage() == 4 || stack.getItemDamage() == 5) {
tooltip.add("Facing: " + getFacing(stack).getName());
String text = Core.proxy.getUpgradeConfigText();
2017-06-12 15:23:32 +02:00
if (!text.isEmpty()) {
tooltip.add(text);
}
}
}
2017-04-11 01:30:27 +02:00
@Override
public void process(
@Nonnull
TileLegacyMachineBase machineBase,
@Nullable
IUpgradeHandler handler,
2017-04-11 01:30:27 +02:00
@Nonnull
ItemStack stack) {
2017-06-12 15:23:32 +02:00
if (stack.getItemDamage() == 0) {
handler.addSpeedMulti(0.25);
handler.addPowerMulti(0.5);
2017-04-11 01:30:27 +02:00
}
2017-06-12 15:23:32 +02:00
if (machineBase instanceof TilePowerAcceptor) {
2017-04-11 20:26:46 +02:00
if (stack.getItemDamage() == 2) {
TilePowerAcceptor acceptor = (TilePowerAcceptor) machineBase;
2017-04-12 20:58:03 +02:00
acceptor.extraPowerStoage += 40000;
}
if (stack.getItemDamage() == 1) {
TilePowerAcceptor acceptor = (TilePowerAcceptor) machineBase;
acceptor.extraTeir += 1;
2017-04-11 20:26:46 +02:00
}
}
2017-04-11 01:30:27 +02:00
}
@Override
@SideOnly(Side.CLIENT)
2017-04-13 12:26:16 +02:00
public void handleRightClick(TileEntity tile, ItemStack stack, Container container, int slotID) {
2017-06-12 15:23:32 +02:00
if (tile.getWorld().isRemote) {
if (stack.getItemDamage() == 4 || stack.getItemDamage() == 5) {
//TODO use the full gui handler
Minecraft.getMinecraft().displayGuiScreen(new GuiSideConfig(Minecraft.getMinecraft().player, tile, getContainer(Minecraft.getMinecraft().player), slotID));
}
}
}
@SuppressWarnings("deprecation")
@SideOnly(Side.CLIENT)
2017-06-12 15:23:32 +02:00
public BuiltContainer getContainer(EntityPlayer player) {
return new ContainerBuilder("sides").create();
}
2017-06-12 15:23:32 +02:00
public EnumFacing getFacing(ItemStack stack) {
return EnumFacing.VALUES[ItemNBTHelper.getInt(stack, "side", 0)];
}
2017-06-12 15:23:32 +02:00
}