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;
|
|
|
|
|
2021-11-14 19:18:07 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.config.CommonConfig;
|
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;
|
2021-03-01 21:23:52 +01:00
|
|
|
import net.minecraft.block.BlockState;
|
|
|
|
import net.minecraft.block.Blocks;
|
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;
|
2021-04-19 21:20:23 +02:00
|
|
|
import net.minecraft.entity.item.ItemEntity;
|
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.item.ItemStack;
|
2021-03-01 21:23:52 +01:00
|
|
|
import net.minecraft.item.Items;
|
2021-02-26 22:15:48 +01:00
|
|
|
import net.minecraft.nbt.CompoundNBT;
|
2021-04-19 21:20:23 +02:00
|
|
|
import net.minecraft.particles.ParticleTypes;
|
|
|
|
import net.minecraft.state.properties.BlockStateProperties;
|
|
|
|
import net.minecraft.util.*;
|
2016-06-02 19:28:51 +02:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2021-04-19 21:20:23 +02:00
|
|
|
import net.minecraft.util.math.BlockRayTraceResult;
|
2016-06-02 19:28:51 +02:00
|
|
|
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;
|
2021-03-01 21:23:52 +01:00
|
|
|
import net.minecraftforge.eventbus.api.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
|
|
|
|
2021-03-01 21:23:52 +01:00
|
|
|
public ItemWaterBowl() {
|
2021-08-22 17:09:06 +02:00
|
|
|
super(ActuallyItems.defaultProps().stacksTo(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) {
|
2021-11-14 19:18:07 +01:00
|
|
|
if (CommonConfig.OTHER.WATER_BOWL.get()) {
|
2019-05-02 09:10:29 +02:00
|
|
|
if (StackUtil.isValid(event.getItemStack()) && event.getItemStack().getItem() == Items.BOWL) {
|
2021-04-19 21:20:23 +02:00
|
|
|
RayTraceResult rayTrace = WorldUtil.getNearestBlockWithDefaultReachDistance(event.getWorld(), event.getPlayer(), true, false, false);
|
|
|
|
if (rayTrace.getType() != RayTraceResult.Type.BLOCK) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockRayTraceResult trace = (BlockRayTraceResult) rayTrace;
|
2021-03-01 21:23:52 +01:00
|
|
|
ActionResult<ItemStack> result = ForgeEventFactory.onBucketUse(event.getPlayer(), event.getWorld(), event.getItemStack(), trace);
|
2021-04-19 21:20:23 +02:00
|
|
|
if (result == null) {
|
2021-08-22 17:09:06 +02:00
|
|
|
if (event.getPlayer().mayUseItemAt(trace.getBlockPos().relative(trace.getDirection()), trace.getDirection(), event.getItemStack())) {
|
|
|
|
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
|
|
|
|
2021-04-19 21:20:23 +02:00
|
|
|
// TODO: Validate fluid check
|
2021-08-22 17:09:06 +02:00
|
|
|
if ((block == Blocks.WATER) && state.getValue(BlockStateProperties.LEVEL) == 0) {
|
|
|
|
event.getPlayer().playSound(SoundEvents.BUCKET_FILL, 1.0F, 1.0F);
|
2016-07-03 20:57:00 +02:00
|
|
|
|
2021-08-22 17:09:06 +02:00
|
|
|
if (!event.getWorld().isClientSide) {
|
|
|
|
event.getWorld().setBlock(trace.getBlockPos(), Blocks.AIR.defaultBlockState(), 11);
|
2018-06-23 01:39:30 +02:00
|
|
|
ItemStack reduced = StackUtil.shrink(event.getItemStack(), 1);
|
2016-07-03 20:57:00 +02:00
|
|
|
|
2021-05-02 18:10:21 +02:00
|
|
|
ItemStack bowl = new ItemStack(ActuallyItems.WATER_BOWL.get());
|
2019-05-02 09:10:29 +02:00
|
|
|
if (!StackUtil.isValid(reduced)) {
|
2021-08-22 17:09:06 +02:00
|
|
|
event.getPlayer().setItemInHand(event.getHand(), bowl);
|
|
|
|
} else if (!event.getPlayer().inventory.add(bowl.copy())) {
|
|
|
|
ItemEntity entityItem = new ItemEntity(event.getWorld(), event.getPlayer().getX(), event.getPlayer().getY(), event.getPlayer().getZ(), bowl.copy());
|
|
|
|
entityItem.setPickUpDelay(0);
|
|
|
|
event.getWorld().addFreshEntity(entityItem);
|
2016-07-03 20:57:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-02 19:28:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-08-22 17:09:06 +02:00
|
|
|
public ActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
|
|
|
|
ItemStack stack = player.getItemInHand(hand);
|
2016-11-19 21:11:17 +01:00
|
|
|
|
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) {
|
2021-08-22 17:09:06 +02:00
|
|
|
return ActionResult.pass(stack);
|
2021-04-19 21:20:23 +02:00
|
|
|
} else if (trace.getType() != RayTraceResult.Type.BLOCK) {
|
2021-08-22 17:09:06 +02:00
|
|
|
return ActionResult.pass(stack);
|
2019-05-02 09:10:29 +02:00
|
|
|
} else {
|
2021-04-19 21:20:23 +02:00
|
|
|
BlockRayTraceResult blockTrace = (BlockRayTraceResult) trace;
|
2021-08-22 17:09:06 +02:00
|
|
|
BlockPos pos = blockTrace.getBlockPos();
|
2016-06-02 19:28:51 +02:00
|
|
|
|
2021-08-22 17:09:06 +02:00
|
|
|
if (!world.mayInteract(player, pos)) {
|
|
|
|
return ActionResult.fail(stack);
|
2019-05-02 09:10:29 +02:00
|
|
|
} else {
|
2021-08-22 17:09:06 +02:00
|
|
|
BlockPos pos1 = world.getBlockState(pos).getMaterial().isReplaceable() && blockTrace.getDirection() == Direction.UP
|
2021-02-26 22:15:48 +01:00
|
|
|
? pos
|
2021-08-22 17:09:06 +02:00
|
|
|
: pos.relative(blockTrace.getDirection());
|
2016-06-02 19:28:51 +02:00
|
|
|
|
2021-08-22 17:09:06 +02:00
|
|
|
if (!player.mayUseItemAt(pos1, blockTrace.getDirection(), stack)) {
|
|
|
|
return ActionResult.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-08-22 17:09:06 +02:00
|
|
|
? ActionResult.success(new ItemStack(Items.BOWL))
|
|
|
|
: ActionResult.success(stack);
|
2019-05-02 09:10:29 +02:00
|
|
|
} else {
|
2021-08-22 17:09:06 +02:00
|
|
|
return ActionResult.fail(stack);
|
2016-06-02 19:28:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-28 01:49:57 +02:00
|
|
|
@Override
|
2021-04-19 21:20:23 +02:00
|
|
|
public void inventoryTick(ItemStack stack, World world, Entity entity, int itemSlot, boolean isSelected) {
|
2021-08-22 17:09:06 +02:00
|
|
|
if (!world.isClientSide) {
|
2021-11-14 19:18:07 +01:00
|
|
|
if (CommonConfig.OTHER.WATER_BOWL_LOSS.get()) {
|
2021-08-22 17:09:06 +02:00
|
|
|
if (world.getGameTime() % 10 == 0 && world.random.nextFloat() >= 0.5F) {
|
2017-06-16 22:38:09 +02:00
|
|
|
int lastX = 0;
|
|
|
|
int lastY = 0;
|
|
|
|
|
2021-04-19 21:20:23 +02:00
|
|
|
if (stack.hasTag()) {
|
|
|
|
CompoundNBT compound = stack.getOrCreateTag();
|
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;
|
2021-08-22 17:09:06 +02:00
|
|
|
if (lastX != 0 && lastX != (int) entity.getX() || lastY != 0 && lastY != (int) entity.getY()) {
|
|
|
|
if (!entity.isShiftKeyDown()) {
|
2021-02-26 22:15:48 +01:00
|
|
|
if (entity instanceof PlayerEntity) {
|
|
|
|
PlayerEntity player = (PlayerEntity) entity;
|
2021-08-22 17:09:06 +02:00
|
|
|
if (this.tryPlaceContainedLiquid(player, world, player.blockPosition(), 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) {
|
2021-04-19 21:20:23 +02:00
|
|
|
CompoundNBT compound = stack.getOrCreateTag();
|
2021-08-22 17:09:06 +02:00
|
|
|
compound.putInt("lastX", (int) entity.getX());
|
|
|
|
compound.putInt("lastY", (int) entity.getY());
|
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) {
|
2021-08-22 17:09:06 +02:00
|
|
|
if (player.inventory.getItem(slot) == old) {
|
|
|
|
player.inventory.setItem(slot, stack);
|
|
|
|
} else if (player.inventory.offhand.get(slot) == old) {
|
|
|
|
player.inventory.offhand.set(slot, stack);
|
2021-02-26 22:15:48 +01:00
|
|
|
}
|
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) {
|
2021-08-22 17:09:06 +02:00
|
|
|
return !ItemStack.isSame(oldStack, newStack);
|
2017-06-16 22:38:09 +02:00
|
|
|
}
|
|
|
|
|
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();
|
2021-04-19 21:20:23 +02:00
|
|
|
boolean replaceable = state.getMaterial().isReplaceable();
|
2016-06-02 19:28:51 +02:00
|
|
|
|
2021-08-22 17:09:06 +02:00
|
|
|
if (!world.isEmptyBlock(pos) && !nonSolid && !replaceable) {
|
2016-06-02 19:28:51 +02:00
|
|
|
return false;
|
2019-05-02 09:10:29 +02:00
|
|
|
} else {
|
2021-08-22 17:09:06 +02:00
|
|
|
if (world.dimensionType().ultraWarm()) {
|
|
|
|
world.playSound(player, pos, SoundEvents.FIRE_EXTINGUISH, SoundCategory.BLOCKS, 0.5F, 2.6F + (world.random.nextFloat() - world.random.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++) {
|
2021-04-19 21:20:23 +02:00
|
|
|
world.addParticle(ParticleTypes.LARGE_SMOKE, 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 {
|
2021-08-22 17:09:06 +02:00
|
|
|
if (!world.isClientSide && (nonSolid || replaceable) && !material.isLiquid()) {
|
2016-06-02 19:28:51 +02:00
|
|
|
world.destroyBlock(pos, true);
|
|
|
|
}
|
|
|
|
|
2021-08-22 17:09:06 +02:00
|
|
|
world.playSound(player, pos, SoundEvents.BUCKET_EMPTY, SoundCategory.BLOCKS, 1.0F, 1.0F);
|
2017-05-28 01:49:57 +02:00
|
|
|
|
2021-08-22 17:09:06 +02:00
|
|
|
BlockState placeState = Blocks.WATER.defaultBlockState();
|
|
|
|
world.setBlock(pos, placeState, 3);
|
2016-06-02 19:28:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|