Unicopia/src/main/java/com/minelittlepony/unicopia/item/PredicatedBlockItem.java

42 lines
1.3 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia.item;
2020-01-16 16:46:24 +01:00
import java.util.function.Predicate;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
2020-04-26 14:46:03 +02:00
import net.minecraft.entity.Entity;
2020-01-16 16:46:24 +01:00
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
public class PredicatedBlockItem extends BlockItem {
2020-04-26 14:46:03 +02:00
private final Predicate<Entity> abilityTest;
2020-01-16 16:46:24 +01:00
2020-04-26 14:46:03 +02:00
public PredicatedBlockItem(Block block, Item.Settings settings, Predicate<Entity> abilityTest) {
2020-01-16 16:46:24 +01:00
super(block, settings);
this.abilityTest = abilityTest;
}
@Override
protected boolean canPlace(ItemPlacementContext context, BlockState state) {
if (context.getPlayer() != null && !(context.getPlayer().abilities.creativeMode || abilityTest.test(context.getPlayer()))) {
return false;
}
return super.canPlace(context, state);
}
@Override
public boolean canMine(BlockState state, World world, BlockPos pos, PlayerEntity player) {
if (!(player.abilities.creativeMode || abilityTest.test(player))) {
return false;
}
return super.canMine(state, world, pos, player);
}
}