2018-11-07 13:33:49 +01:00
|
|
|
package de.ellpeck.naturesaura.items;
|
|
|
|
|
2018-11-23 19:21:52 +01:00
|
|
|
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
|
|
|
|
import de.ellpeck.naturesaura.api.multiblock.IMultiblock;
|
2021-12-04 15:40:09 +01:00
|
|
|
import net.minecraft.entity.player.Player;
|
2018-11-07 13:33:49 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
2019-11-04 19:08:49 +01:00
|
|
|
import net.minecraft.item.ItemUseContext;
|
2018-11-07 13:33:49 +01:00
|
|
|
import net.minecraft.util.ActionResult;
|
2021-12-04 15:40:09 +01:00
|
|
|
import net.minecraft.util.InteractionResult;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraft.util.Hand;
|
2019-11-04 19:08:49 +01:00
|
|
|
import net.minecraft.util.text.ITextComponent;
|
2020-09-22 03:17:02 +02:00
|
|
|
import net.minecraft.util.text.TextComponent;
|
2021-12-04 15:40:09 +01:00
|
|
|
import net.minecraft.level.Level;
|
2018-11-07 13:33:49 +01:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2020-01-26 01:41:49 +01:00
|
|
|
public class ItemMultiblockMaker extends ItemImpl {
|
2018-11-07 13:33:49 +01:00
|
|
|
|
2018-11-23 19:21:52 +01:00
|
|
|
private static List<IMultiblock> multiblocks;
|
2018-11-07 13:33:49 +01:00
|
|
|
|
2020-01-26 01:41:49 +01:00
|
|
|
public ItemMultiblockMaker() {
|
2020-02-07 23:50:16 +01:00
|
|
|
super("multiblock_maker");
|
2018-11-07 13:33:49 +01:00
|
|
|
}
|
|
|
|
|
2020-02-07 15:22:30 +01:00
|
|
|
private static int getMultiblock(ItemStack stack) {
|
|
|
|
if (!stack.hasTag())
|
|
|
|
return -1;
|
|
|
|
return stack.getTag().getInt("multiblock");
|
|
|
|
}
|
|
|
|
|
|
|
|
private static List<IMultiblock> multiblocks() {
|
|
|
|
if (multiblocks == null) {
|
|
|
|
multiblocks = new ArrayList<>();
|
|
|
|
multiblocks.addAll(NaturesAuraAPI.MULTIBLOCKS.values());
|
|
|
|
}
|
|
|
|
return multiblocks;
|
|
|
|
}
|
|
|
|
|
2018-11-07 13:33:49 +01:00
|
|
|
@Override
|
2021-12-04 15:40:09 +01:00
|
|
|
public ActionResult<ItemStack> onItemRightClick(Level levelIn, Player playerIn, Hand handIn) {
|
2018-11-07 13:33:49 +01:00
|
|
|
ItemStack stack = playerIn.getHeldItem(handIn);
|
2021-12-04 15:40:09 +01:00
|
|
|
if (!levelIn.isClientSide && playerIn.isCreative()) {
|
2018-11-07 13:33:49 +01:00
|
|
|
int curr = getMultiblock(stack);
|
|
|
|
int next = (curr + 1) % multiblocks().size();
|
2019-11-04 19:08:49 +01:00
|
|
|
stack.getOrCreateTag().putInt("multiblock", next);
|
2018-11-07 13:33:49 +01:00
|
|
|
}
|
2021-12-04 15:40:09 +01:00
|
|
|
return new ActionResult<>(InteractionResult.SUCCESS, stack);
|
2018-11-07 13:33:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-04 15:40:09 +01:00
|
|
|
public InteractionResult onItemUse(ItemUseContext context) {
|
|
|
|
Player player = context.getPlayer();
|
2019-11-04 19:08:49 +01:00
|
|
|
if (player.isCreative()) {
|
2020-01-22 01:32:26 +01:00
|
|
|
int id = getMultiblock(player.getHeldItem(context.getHand()));
|
|
|
|
if (id < 0)
|
2021-12-04 15:40:09 +01:00
|
|
|
return InteractionResult.PASS;
|
2020-01-22 01:32:26 +01:00
|
|
|
IMultiblock multi = multiblocks().get(id);
|
2018-11-07 13:33:49 +01:00
|
|
|
if (multi == null)
|
2021-12-04 15:40:09 +01:00
|
|
|
return InteractionResult.PASS;
|
2018-11-07 13:33:49 +01:00
|
|
|
|
2021-12-04 15:40:09 +01:00
|
|
|
if (!context.getLevel().isClientSide)
|
2019-11-04 19:08:49 +01:00
|
|
|
multi.forEach(context.getPos().up(), (char) 0, (blockPos, matcher) -> {
|
2021-12-04 15:40:09 +01:00
|
|
|
context.getLevel().setBlockState(blockPos, matcher.getDefaultState());
|
2018-11-07 13:38:10 +01:00
|
|
|
return true;
|
|
|
|
});
|
2018-11-07 13:33:49 +01:00
|
|
|
|
2021-12-04 15:40:09 +01:00
|
|
|
return InteractionResult.SUCCESS;
|
2018-11-07 13:33:49 +01:00
|
|
|
}
|
2021-12-04 15:40:09 +01:00
|
|
|
return InteractionResult.PASS;
|
2018-11-07 13:33:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-11-04 19:08:49 +01:00
|
|
|
public ITextComponent getDisplayName(ItemStack stack) {
|
2020-09-22 03:17:02 +02:00
|
|
|
TextComponent name = (TextComponent) super.getDisplayName(stack);
|
2020-01-22 01:32:26 +01:00
|
|
|
int id = getMultiblock(stack);
|
|
|
|
if (id < 0)
|
|
|
|
return name;
|
|
|
|
IMultiblock multi = multiblocks().get(id);
|
2020-09-22 03:17:02 +02:00
|
|
|
return multi == null ? name : name.appendString(" (" + multi.getName() + ")");
|
2018-11-07 13:33:49 +01:00
|
|
|
}
|
|
|
|
}
|