2016-06-02 19:28:51 +02:00
|
|
|
/*
|
|
|
|
* This file ("ItemWaterBowl.java") is part of the Actually Additions mod for Minecraft.
|
|
|
|
* It is created and owned by Ellpeck and distributed
|
|
|
|
* under the Actually Additions License to be found at
|
|
|
|
* http://ellpeck.de/actaddlicense
|
|
|
|
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
|
|
|
|
*
|
2017-01-01 16:23:26 +01:00
|
|
|
* © 2015-2017 Ellpeck
|
2016-06-02 19:28:51 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
package de.ellpeck.actuallyadditions.mod.items;
|
|
|
|
|
2016-07-03 20:57:00 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.config.values.ConfigBoolValues;
|
2016-06-02 19:28:51 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.items.base.ItemBase;
|
2016-11-16 16:59:00 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
|
2016-06-02 19:28:51 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
|
2017-03-28 17:09:45 +02:00
|
|
|
import net.minecraft.block.Block;
|
2016-07-03 20:57:00 +02:00
|
|
|
import net.minecraft.block.BlockLiquid;
|
2016-06-02 19:28:51 +02:00
|
|
|
import net.minecraft.block.material.Material;
|
2017-05-28 01:49:57 +02:00
|
|
|
import net.minecraft.entity.Entity;
|
2016-07-03 20:57:00 +02:00
|
|
|
import net.minecraft.entity.item.EntityItem;
|
2021-02-26 22:15:48 +01:00
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
2016-06-02 19:28:51 +02:00
|
|
|
import net.minecraft.init.Blocks;
|
|
|
|
import net.minecraft.init.Items;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2021-02-26 22:15:48 +01:00
|
|
|
import net.minecraft.nbt.CompoundNBT;
|
2019-05-02 09:10:29 +02:00
|
|
|
import net.minecraft.util.ActionResult;
|
2021-02-26 22:15:48 +01:00
|
|
|
import net.minecraft.util.Hand;
|
2019-05-02 09:10:29 +02:00
|
|
|
import net.minecraft.util.SoundCategory;
|
2021-02-26 22:15:48 +01:00
|
|
|
import net.minecraft.util.SoundEvents;
|
2016-06-02 19:28:51 +02:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
import net.minecraft.util.math.RayTraceResult;
|
|
|
|
import net.minecraft.world.World;
|
2016-07-03 20:57:00 +02:00
|
|
|
import net.minecraftforge.common.MinecraftForge;
|
2016-06-02 19:28:51 +02:00
|
|
|
import net.minecraftforge.event.ForgeEventFactory;
|
2016-07-03 20:57:00 +02:00
|
|
|
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
|
|
|
|
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
2016-06-02 19:28:51 +02:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public class ItemWaterBowl extends ItemBase {
|
2016-06-02 19:28:51 +02:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public ItemWaterBowl(String name) {
|
2016-06-02 19:28:51 +02:00
|
|
|
super(name);
|
|
|
|
this.setMaxStackSize(1);
|
2016-07-03 20:57:00 +02:00
|
|
|
|
|
|
|
MinecraftForge.EVENT_BUS.register(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@SubscribeEvent
|
2019-05-02 09:10:29 +02:00
|
|
|
public void onPlayerInteractEvent(PlayerInteractEvent.RightClickItem event) {
|
|
|
|
if (event.getWorld() != null) {
|
|
|
|
if (ConfigBoolValues.WATER_BOWL.isEnabled()) {
|
|
|
|
if (StackUtil.isValid(event.getItemStack()) && event.getItemStack().getItem() == Items.BOWL) {
|
2016-07-03 20:57:00 +02:00
|
|
|
RayTraceResult trace = WorldUtil.getNearestBlockWithDefaultReachDistance(event.getWorld(), event.getEntityPlayer(), true, false, false);
|
|
|
|
ActionResult<ItemStack> result = ForgeEventFactory.onBucketUse(event.getEntityPlayer(), event.getWorld(), event.getItemStack(), trace);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (result == null && trace != null && trace.getBlockPos() != null) {
|
|
|
|
if (event.getEntityPlayer().canPlayerEdit(trace.getBlockPos().offset(trace.sideHit), trace.sideHit, event.getItemStack())) {
|
2021-02-26 22:15:48 +01:00
|
|
|
BlockState state = event.getWorld().getBlockState(trace.getBlockPos());
|
2017-03-28 17:09:45 +02:00
|
|
|
Block block = state.getBlock();
|
2016-07-03 20:57:00 +02:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if ((block == Blocks.WATER || block == Blocks.FLOWING_WATER) && state.getValue(BlockLiquid.LEVEL) == 0) {
|
2016-07-03 20:57:00 +02:00
|
|
|
event.getEntityPlayer().playSound(SoundEvents.ITEM_BUCKET_FILL, 1.0F, 1.0F);
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (!event.getWorld().isRemote) {
|
2016-07-03 20:57:00 +02:00
|
|
|
event.getWorld().setBlockState(trace.getBlockPos(), Blocks.AIR.getDefaultState(), 11);
|
2018-06-23 01:39:30 +02:00
|
|
|
ItemStack reduced = StackUtil.shrink(event.getItemStack(), 1);
|
2016-07-03 20:57:00 +02:00
|
|
|
|
|
|
|
ItemStack bowl = new ItemStack(InitItems.itemWaterBowl);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (!StackUtil.isValid(reduced)) {
|
2016-07-20 22:31:27 +02:00
|
|
|
event.getEntityPlayer().setHeldItem(event.getHand(), bowl);
|
2019-05-02 09:10:29 +02:00
|
|
|
} else if (!event.getEntityPlayer().inventory.addItemStackToInventory(bowl.copy())) {
|
2016-07-03 20:57:00 +02:00
|
|
|
EntityItem entityItem = new EntityItem(event.getWorld(), event.getEntityPlayer().posX, event.getEntityPlayer().posY, event.getEntityPlayer().posZ, bowl.copy());
|
|
|
|
entityItem.setPickupDelay(0);
|
2016-11-26 21:32:27 +01:00
|
|
|
event.getWorld().spawnEntity(entityItem);
|
2016-07-03 20:57:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-02 19:28:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-26 22:15:48 +01:00
|
|
|
public ActionResult<ItemStack> onItemRightClick(World world, PlayerEntity player, Hand hand) {
|
2016-11-19 21:11:17 +01:00
|
|
|
ItemStack stack = player.getHeldItem(hand);
|
|
|
|
|
2016-06-02 19:28:51 +02:00
|
|
|
RayTraceResult trace = WorldUtil.getNearestBlockWithDefaultReachDistance(world, player);
|
|
|
|
ActionResult<ItemStack> result = ForgeEventFactory.onBucketUse(player, world, stack, trace);
|
2021-02-26 22:15:48 +01:00
|
|
|
if (result != null) {
|
|
|
|
return result;
|
|
|
|
}
|
2016-06-02 19:28:51 +02:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (trace == null) {
|
2019-02-27 19:53:05 +01:00
|
|
|
return new ActionResult<>(EnumActionResult.PASS, stack);
|
2019-05-02 09:10:29 +02:00
|
|
|
} else if (trace.typeOfHit != RayTraceResult.Type.BLOCK) {
|
2019-02-27 19:53:05 +01:00
|
|
|
return new ActionResult<>(EnumActionResult.PASS, stack);
|
2019-05-02 09:10:29 +02:00
|
|
|
} else {
|
2016-06-02 19:28:51 +02:00
|
|
|
BlockPos pos = trace.getBlockPos();
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (!world.isBlockModifiable(player, pos)) {
|
2019-02-27 19:53:05 +01:00
|
|
|
return new ActionResult<>(EnumActionResult.FAIL, stack);
|
2019-05-02 09:10:29 +02:00
|
|
|
} else {
|
2021-02-27 13:24:45 +01:00
|
|
|
BlockPos pos1 = world.getBlockState(pos).getBlock().isReplaceable(world, pos) && trace.sideHit == Direction.UP
|
2021-02-26 22:15:48 +01:00
|
|
|
? pos
|
|
|
|
: pos.offset(trace.sideHit);
|
2016-06-02 19:28:51 +02:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (!player.canPlayerEdit(pos1, trace.sideHit, stack)) {
|
2019-02-27 19:53:05 +01:00
|
|
|
return new ActionResult<>(EnumActionResult.FAIL, stack);
|
2019-05-02 09:10:29 +02:00
|
|
|
} else if (this.tryPlaceContainedLiquid(player, world, pos1, false)) {
|
2021-02-28 15:47:54 +01:00
|
|
|
return !player.isCreative()
|
2021-02-26 22:15:48 +01:00
|
|
|
? new ActionResult<>(EnumActionResult.SUCCESS, new ItemStack(Items.BOWL))
|
|
|
|
: new ActionResult<>(EnumActionResult.SUCCESS, stack);
|
2019-05-02 09:10:29 +02:00
|
|
|
} else {
|
2019-02-27 19:53:05 +01:00
|
|
|
return new ActionResult<>(EnumActionResult.FAIL, stack);
|
2016-06-02 19:28:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-28 01:49:57 +02:00
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public void onUpdate(ItemStack stack, World world, Entity entity, int itemSlot, boolean isSelected) {
|
|
|
|
if (!world.isRemote) {
|
|
|
|
if (ConfigBoolValues.WATER_BOWL_LOSS.isEnabled()) {
|
|
|
|
if (world.getTotalWorldTime() % 10 == 0 && world.rand.nextFloat() >= 0.5F) {
|
2017-06-16 22:38:09 +02:00
|
|
|
int lastX = 0;
|
|
|
|
int lastY = 0;
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (stack.hasTagCompound()) {
|
2021-02-26 22:15:48 +01:00
|
|
|
CompoundNBT compound = stack.getTagCompound();
|
2021-02-27 16:33:00 +01:00
|
|
|
lastX = compound.getInt("lastX");
|
|
|
|
lastY = compound.getInt("lastY");
|
2017-06-16 22:38:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
boolean change = false;
|
2019-05-02 09:10:29 +02:00
|
|
|
if (lastX != 0 && lastX != (int) entity.posX || lastY != 0 && lastY != (int) entity.posY) {
|
|
|
|
if (!entity.isSneaking()) {
|
2021-02-26 22:15:48 +01:00
|
|
|
if (entity instanceof PlayerEntity) {
|
|
|
|
PlayerEntity player = (PlayerEntity) entity;
|
2019-05-02 09:10:29 +02:00
|
|
|
if (this.tryPlaceContainedLiquid(player, world, player.getPosition(), true)) {
|
2019-02-27 19:53:05 +01:00
|
|
|
this.checkReplace(player, stack, new ItemStack(Items.BOWL), itemSlot);
|
2017-06-16 22:38:09 +02:00
|
|
|
}
|
2017-05-28 01:49:57 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-16 22:38:09 +02:00
|
|
|
change = true;
|
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (change || lastX == 0 || lastY == 0) {
|
|
|
|
if (!stack.hasTagCompound()) {
|
2021-02-26 22:15:48 +01:00
|
|
|
stack.setTagCompound(new CompoundNBT());
|
2017-06-16 22:38:09 +02:00
|
|
|
}
|
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
CompoundNBT compound = stack.getTagCompound();
|
2021-02-27 16:33:00 +01:00
|
|
|
compound.putInt("lastX", (int) entity.posX);
|
|
|
|
compound.putInt("lastY", (int) entity.posY);
|
2017-05-28 01:49:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-27 19:53:05 +01:00
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
private void checkReplace(PlayerEntity player, ItemStack old, ItemStack stack, int slot) {
|
|
|
|
if (player.inventory.getStackInSlot(slot) == old) {
|
|
|
|
player.inventory.setInventorySlotContents(slot, stack);
|
|
|
|
} else if (player.inventory.offHandInventory.get(slot) == old) {
|
|
|
|
player.inventory.offHandInventory.set(slot, stack);
|
|
|
|
}
|
2018-04-13 19:19:52 +02:00
|
|
|
}
|
2017-05-28 01:49:57 +02:00
|
|
|
|
2017-06-16 22:38:09 +02:00
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public boolean shouldCauseReequipAnimation(ItemStack oldStack, ItemStack newStack, boolean slotChanged) {
|
2017-06-16 22:38:09 +02:00
|
|
|
return !ItemStack.areItemsEqual(oldStack, newStack);
|
|
|
|
}
|
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
public boolean tryPlaceContainedLiquid(PlayerEntity player, World world, BlockPos pos, boolean finite) {
|
|
|
|
BlockState state = world.getBlockState(pos);
|
2016-06-02 19:28:51 +02:00
|
|
|
Material material = state.getMaterial();
|
|
|
|
boolean nonSolid = !material.isSolid();
|
|
|
|
boolean replaceable = state.getBlock().isReplaceable(world, pos);
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (!world.isAirBlock(pos) && !nonSolid && !replaceable) {
|
2016-06-02 19:28:51 +02:00
|
|
|
return false;
|
2019-05-02 09:10:29 +02:00
|
|
|
} else {
|
|
|
|
if (world.provider.doesWaterVaporize()) {
|
|
|
|
world.playSound(player, pos, SoundEvents.BLOCK_FIRE_EXTINGUISH, SoundCategory.BLOCKS, 0.5F, 2.6F + (world.rand.nextFloat() - world.rand.nextFloat()) * 0.8F);
|
2016-06-02 19:28:51 +02:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
for (int k = 0; k < 8; k++) {
|
|
|
|
world.spawnParticle(EnumParticleTypes.SMOKE_LARGE, pos.getX() + Math.random(), pos.getY() + Math.random(), pos.getZ() + Math.random(), 0.0D, 0.0D, 0.0D);
|
2016-06-02 19:28:51 +02:00
|
|
|
}
|
2019-05-02 09:10:29 +02:00
|
|
|
} else {
|
|
|
|
if (!world.isRemote && (nonSolid || replaceable) && !material.isLiquid()) {
|
2016-06-02 19:28:51 +02:00
|
|
|
world.destroyBlock(pos, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
world.playSound(player, pos, SoundEvents.ITEM_BUCKET_EMPTY, SoundCategory.BLOCKS, 1.0F, 1.0F);
|
2017-05-28 01:49:57 +02:00
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
BlockState placeState;
|
2019-05-02 09:10:29 +02:00
|
|
|
if (finite) {
|
2017-05-28 01:49:57 +02:00
|
|
|
placeState = Blocks.FLOWING_WATER.getDefaultState();
|
2019-05-02 09:10:29 +02:00
|
|
|
} else {
|
2017-05-28 01:49:57 +02:00
|
|
|
placeState = Blocks.FLOWING_WATER.getDefaultState();
|
|
|
|
}
|
|
|
|
world.setBlockState(pos, placeState, 3);
|
2016-06-02 19:28:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|