Improve Dispenser Behavior
All checks were successful
SlightlyVanilla/pipeline/head This commit looks good

This commit is contained in:
TheBrokenRail 2020-03-25 17:03:44 -04:00
parent c82406bb47
commit cabf4162ca
3 changed files with 87 additions and 46 deletions

View File

@ -1,5 +1,7 @@
package com.thebrokenrail.slightlyvanilla; package com.thebrokenrail.slightlyvanilla;
import com.thebrokenrail.slightlyvanilla.dispenser.SlimeballDispenserBehavior;
import com.thebrokenrail.slightlyvanilla.dispenser.SpawnEggDispenserBehavior;
import com.thebrokenrail.slightlyvanilla.entity.SlimeballEntity; import com.thebrokenrail.slightlyvanilla.entity.SlimeballEntity;
import com.thebrokenrail.slightlyvanilla.entity.SpawnEggEntity; import com.thebrokenrail.slightlyvanilla.entity.SpawnEggEntity;
import me.sargunvohra.mcmods.autoconfig1u.AutoConfig; import me.sargunvohra.mcmods.autoconfig1u.AutoConfig;
@ -9,26 +11,17 @@ import net.fabricmc.fabric.api.entity.FabricEntityTypeBuilder;
import net.fabricmc.fabric.api.tag.TagRegistry; import net.fabricmc.fabric.api.tag.TagRegistry;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.DispenserBlock; import net.minecraft.block.DispenserBlock;
import net.minecraft.block.dispenser.ItemDispenserBehavior;
import net.minecraft.block.dispenser.ProjectileDispenserBehavior;
import net.minecraft.entity.EntityCategory; import net.minecraft.entity.EntityCategory;
import net.minecraft.entity.EntityDimensions; import net.minecraft.entity.EntityDimensions;
import net.minecraft.entity.EntityType; import net.minecraft.entity.EntityType;
import net.minecraft.entity.SpawnType;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.ProjectileEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items; import net.minecraft.item.Items;
import net.minecraft.item.SpawnEggItem; import net.minecraft.item.SpawnEggItem;
import net.minecraft.sound.SoundCategory; import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents; import net.minecraft.sound.SoundEvents;
import net.minecraft.tag.Tag; import net.minecraft.tag.Tag;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPointer;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Position;
import net.minecraft.util.registry.Registry; import net.minecraft.util.registry.Registry;
import net.minecraft.world.World;
import java.util.Random; import java.util.Random;
@ -54,45 +47,10 @@ public class SlightlyVanilla implements ModInitializer {
Registry.register(Registry.ENTITY_TYPE, new Identifier(NAMESPACE, "slimeball"), SLIMEBALL_ENTITY); Registry.register(Registry.ENTITY_TYPE, new Identifier(NAMESPACE, "slimeball"), SLIMEBALL_ENTITY);
Registry.register(Registry.ENTITY_TYPE, new Identifier(NAMESPACE, "spawn_egg"), SPAWN_EGG_ENTITY); Registry.register(Registry.ENTITY_TYPE, new Identifier(NAMESPACE, "spawn_egg"), SPAWN_EGG_ENTITY);
DispenserBlock.registerBehavior(Items.SLIME_BALL, (pointer, stack) -> { DispenserBlock.registerBehavior(Items.SLIME_BALL, new SlimeballDispenserBehavior());
if (getConfig().throwableSlimeballs.dispenser) {
return new ProjectileDispenserBehavior() {
@Override
protected ProjectileEntity createProjectile(World position, Position stack, ItemStack itemStack) {
SlimeballEntity entity = new SlimeballEntity(position, stack.getX(), stack.getY(), stack.getZ());
entity.setItem(itemStack);
return entity;
}
}.dispense(pointer, stack);
} else {
return new ItemDispenserBehavior().dispense(pointer, stack);
}
});
for (SpawnEggItem spawnEgg : SpawnEggItem.getAll()) { for (SpawnEggItem spawnEgg : SpawnEggItem.getAll()) {
DispenserBlock.registerBehavior(spawnEgg, (pointer, stack) -> { DispenserBlock.registerBehavior(spawnEgg, new SpawnEggDispenserBehavior());
if (getConfig().throwableSpawnEggs.dispenser) {
return new ProjectileDispenserBehavior() {
@Override
protected ProjectileEntity createProjectile(World position, Position stack, ItemStack itemStack) {
SpawnEggEntity entity = new SpawnEggEntity(position, stack.getX(), stack.getY(), stack.getZ());
entity.setItem(itemStack);
return entity;
}
}.dispense(pointer, stack);
} else {
return new ItemDispenserBehavior() {
@Override
protected ItemStack dispenseSilently(BlockPointer pointer, ItemStack stack) {
Direction direction = pointer.getBlockState().get(DispenserBlock.FACING);
EntityType<?> entityType = ((SpawnEggItem)stack.getItem()).getEntityType(stack.getTag());
entityType.spawnFromItemStack(pointer.getWorld(), stack, null, pointer.getBlockPos().offset(direction), SpawnType.DISPENSER, direction != Direction.UP, false);
stack.decrement(1);
return stack;
}
}.dispense(pointer, stack);
}
});
} }
} }

View File

@ -0,0 +1,35 @@
package com.thebrokenrail.slightlyvanilla.dispenser;
import com.thebrokenrail.slightlyvanilla.SlightlyVanilla;
import com.thebrokenrail.slightlyvanilla.entity.SlimeballEntity;
import net.minecraft.block.dispenser.DispenserBehavior;
import net.minecraft.block.dispenser.ItemDispenserBehavior;
import net.minecraft.block.dispenser.ProjectileDispenserBehavior;
import net.minecraft.entity.projectile.ProjectileEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPointer;
import net.minecraft.util.math.Position;
import net.minecraft.world.World;
public class SlimeballDispenserBehavior implements DispenserBehavior {
private static class Default extends ItemDispenserBehavior {
}
private static class Modified extends ProjectileDispenserBehavior {
@Override
protected ProjectileEntity createProjectile(World position, Position stack, ItemStack itemStack) {
SlimeballEntity entity = new SlimeballEntity(position, stack.getX(), stack.getY(), stack.getZ());
entity.setItem(itemStack);
return entity;
}
}
@Override
public ItemStack dispense(BlockPointer pointer, ItemStack stack) {
if (SlightlyVanilla.getConfig().throwableSlimeballs.dispenser) {
return new Modified().dispense(pointer, stack);
} else {
return new Default().dispense(pointer, stack);
}
}
}

View File

@ -0,0 +1,48 @@
package com.thebrokenrail.slightlyvanilla.dispenser;
import com.thebrokenrail.slightlyvanilla.SlightlyVanilla;
import com.thebrokenrail.slightlyvanilla.entity.SpawnEggEntity;
import net.minecraft.block.DispenserBlock;
import net.minecraft.block.dispenser.DispenserBehavior;
import net.minecraft.block.dispenser.ItemDispenserBehavior;
import net.minecraft.block.dispenser.ProjectileDispenserBehavior;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.SpawnType;
import net.minecraft.entity.projectile.ProjectileEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.SpawnEggItem;
import net.minecraft.util.math.BlockPointer;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Position;
import net.minecraft.world.World;
public class SpawnEggDispenserBehavior implements DispenserBehavior {
private static class Default extends ItemDispenserBehavior {
@Override
protected ItemStack dispenseSilently(BlockPointer pointer, ItemStack stack) {
Direction direction = pointer.getBlockState().get(DispenserBlock.FACING);
EntityType<?> entityType = ((SpawnEggItem)stack.getItem()).getEntityType(stack.getTag());
entityType.spawnFromItemStack(pointer.getWorld(), stack, null, pointer.getBlockPos().offset(direction), SpawnType.DISPENSER, direction != Direction.UP, false);
stack.decrement(1);
return stack;
}
}
private static class Modified extends ProjectileDispenserBehavior {
@Override
protected ProjectileEntity createProjectile(World position, Position stack, ItemStack itemStack) {
SpawnEggEntity entity = new SpawnEggEntity(position, stack.getX(), stack.getY(), stack.getZ());
entity.setItem(itemStack);
return entity;
}
}
@Override
public ItemStack dispense(BlockPointer pointer, ItemStack stack) {
if (SlightlyVanilla.getConfig().throwableSpawnEggs.dispenser) {
return new Modified().dispense(pointer, stack);
} else {
return new Default().dispense(pointer, stack);
}
}
}