Last active
March 25, 2020 21:53
-
-
Save Lanse505/d55bb6b3b3ad4431e2551ee7a65aba6f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.teamacronymcoders.essence.items.misc.wrench; | |
| import com.teamacronymcoders.essence.Essence; | |
| import net.minecraft.block.BlockState; | |
| import net.minecraft.block.Blocks; | |
| import net.minecraft.client.util.ITooltipFlag; | |
| import net.minecraft.entity.item.ItemEntity; | |
| import net.minecraft.entity.player.PlayerEntity; | |
| import net.minecraft.item.Item; | |
| import net.minecraft.item.ItemStack; | |
| import net.minecraft.item.ItemUseContext; | |
| import net.minecraft.item.Rarity; | |
| import net.minecraft.nbt.CompoundNBT; | |
| import net.minecraft.state.IProperty; | |
| import net.minecraft.tileentity.TileEntity; | |
| import net.minecraft.util.ActionResultType; | |
| import net.minecraft.util.Rotation; | |
| import net.minecraft.util.math.BlockPos; | |
| import net.minecraft.util.text.ITextComponent; | |
| import net.minecraft.util.text.TranslationTextComponent; | |
| import net.minecraft.world.World; | |
| import net.minecraftforge.common.util.Constants; | |
| import javax.annotation.Nullable; | |
| import java.util.List; | |
| public class EssenceWrench extends Item { | |
| private final WrenchModeEnum mode; | |
| public EssenceWrench() { | |
| super(new Item.Properties().group(Essence.TOOL_TAB).maxStackSize(1).maxDamage(0).rarity(Rarity.RARE)); | |
| this.mode = WrenchModeEnum.SERIALIZE; | |
| } | |
| @Override | |
| public ActionResultType onItemUseFirst(ItemStack stack, ItemUseContext context) { | |
| World world = context.getWorld(); | |
| BlockPos pos = context.getPos(); | |
| BlockState state = world.getBlockState(pos); | |
| PlayerEntity player = context.getPlayer(); | |
| if (mode == WrenchModeEnum.SERIALIZE) { | |
| TileEntity te = world.getTileEntity(pos); | |
| ItemStack drop = new ItemStack(state.getBlock()); | |
| CompoundNBT stateNBT = new CompoundNBT(); | |
| state.getProperties().forEach(iProperty -> stateNBT.putString(iProperty.getName(), getStatePropertyValue(state, iProperty))); | |
| drop.setTagInfo("BlockStateTag", stateNBT); | |
| if (te != null) { | |
| drop.setTagInfo("BlockEntityTag", te.serializeNBT()); | |
| } | |
| world.setBlockState(pos, Blocks.AIR.getDefaultState(), Constants.BlockFlags.DEFAULT_AND_RERENDER); | |
| ItemEntity itemEntity = new ItemEntity(world, pos.getX(), pos.getY(), pos.getZ(), drop); | |
| itemEntity.setDefaultPickupDelay(); | |
| world.addEntity(itemEntity); | |
| return ActionResultType.SUCCESS; | |
| } | |
| if (mode == WrenchModeEnum.ROTATE) { | |
| if (player != null && player.isShiftKeyDown()) { | |
| state.rotate(Rotation.CLOCKWISE_180); | |
| } | |
| state.rotate(Rotation.CLOCKWISE_90); | |
| return ActionResultType.SUCCESS; | |
| } | |
| return ActionResultType.PASS; | |
| } | |
| private static <T extends Comparable<T>> String getStatePropertyValue(BlockState state, IProperty<T> property) { | |
| T prop = state.get(property); | |
| return property.getName(prop); | |
| } | |
| @Override | |
| public void addInformation(ItemStack stack, @Nullable World world, List<ITextComponent> list, ITooltipFlag flag) { | |
| list.add(new TranslationTextComponent("essence.wrench.mode.tooltip").appendText(": ").appendSibling(new TranslationTextComponent(mode.getLocaleName()))); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment