Rewrite poss ball

This commit is contained in:
Tara Piccari 2024-01-30 21:01:47 -07:00
parent 83728faa68
commit 1e9da88ba4
4 changed files with 80 additions and 76 deletions

View file

@ -49,7 +49,7 @@ mod_name=Thresholds
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=GPLv3 mod_license=GPLv3
# The mod version. See https://semver.org/ # The mod version. See https://semver.org/
mod_version=1.4.013024.1744 mod_version=1.4.013024.2101
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. # The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
# This should match the base package used for the mod sources. # This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html # See https://maven.apache.org/guides/mini/guide-naming-conventions.html

View file

@ -6,10 +6,7 @@ import net.minecraft.sounds.SoundSource;
import net.minecraft.stats.Stats; import net.minecraft.stats.Stats;
import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder; import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.monster.Guardian;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.projectile.Arrow;
import net.minecraft.world.item.EggItem;
import net.minecraft.world.item.Item; import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
@ -19,9 +16,10 @@ public class PossBallItem extends Item
public PossBallItem(Properties pProperties) { public PossBallItem(Properties pProperties) {
super(pProperties); super(pProperties);
} }
@Override @Override
public boolean isFoil(ItemStack pStack) { public boolean isFoil(ItemStack pStack) {
if(!pStack.hasTag()) if(!pStack.hasTag() || pStack.getTag()==null)
{ {
pStack.setTag(new CompoundTag()); pStack.setTag(new CompoundTag());
} }
@ -37,21 +35,17 @@ public class PossBallItem extends Item
pLevel.playSound((Player) null, pPlayer.getX(), pPlayer.getY(), pPlayer.getZ(), SoundEvents.EGG_THROW, SoundSource.PLAYERS, 0.5F, 0.4F / (pLevel.random.nextFloat() * 0.4f + 0.8f)); pLevel.playSound((Player) null, pPlayer.getX(), pPlayer.getY(), pPlayer.getZ(), SoundEvents.EGG_THROW, SoundSource.PLAYERS, 0.5F, 0.4F / (pLevel.random.nextFloat() * 0.4f + 0.8f));
if(!pLevel.isClientSide) if(!pLevel.isClientSide)
{ {
ThrownPossBall TPB = new ThrownPossBall(pLevel, pPlayer); ItemStack single = stack.copy();
if(pPlayer.getAbilities().instabuild) TPB.setItem(stack.copy()); single.setCount(1);
else
TPB.setItem(stack.copy()); stack.shrink(1);
ThrownPossBall TPB = new ThrownPossBall(pLevel, pPlayer, single);
TPB.shootFromRotation(pPlayer, pPlayer.getXRot(), pPlayer.getYRot(), 0.0F, 1.5F, 1.0F); TPB.shootFromRotation(pPlayer, pPlayer.getXRot(), pPlayer.getYRot(), 0.0F, 1.5F, 1.0F);
pLevel.addFreshEntity(TPB); pLevel.addFreshEntity(TPB);
} }
pPlayer.awardStat(Stats.ITEM_USED.get(this)); pPlayer.awardStat(Stats.ITEM_USED.get(this));
if(!pPlayer.getAbilities().instabuild)
{
stack.shrink(1);
}else {
}
return super.use(pLevel, pPlayer, pUsedHand); return super.use(pLevel, pPlayer, pUsedHand);
} }
} }

View file

@ -1,17 +1,12 @@
package dev.zontreck.otemod.items; package dev.zontreck.otemod.items;
import dev.zontreck.libzontreck.chat.ChatColor; import dev.zontreck.libzontreck.chat.ChatColor;
import dev.zontreck.libzontreck.lore.ExtraLore;
import dev.zontreck.libzontreck.lore.LoreContainer; import dev.zontreck.libzontreck.lore.LoreContainer;
import dev.zontreck.libzontreck.lore.LoreEntry; import dev.zontreck.libzontreck.lore.LoreEntry;
import dev.zontreck.libzontreck.util.ChatHelpers;
import dev.zontreck.libzontreck.util.ServerUtilities; import dev.zontreck.libzontreck.util.ServerUtilities;
import net.minecraft.core.particles.ItemParticleOption; import net.minecraft.core.particles.ItemParticleOption;
import net.minecraft.core.particles.ParticleTypes; import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.LivingEntity;
@ -21,7 +16,6 @@ import net.minecraft.world.entity.projectile.ThrowableItemProjectile;
import net.minecraft.world.item.Item; import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.DropperBlock;
import net.minecraft.world.phys.EntityHitResult; import net.minecraft.world.phys.EntityHitResult;
import net.minecraft.world.phys.HitResult; import net.minecraft.world.phys.HitResult;
@ -32,28 +26,40 @@ public class ThrownPossBall extends ThrowableItemProjectile
{ {
boolean captured = false; boolean captured = false;
LivingEntity shooter; LivingEntity shooter;
ItemStack self;
public ThrownPossBall(EntityType<? extends ThrownPossBall> entity, Level level) public ThrownPossBall(EntityType<? extends ThrownPossBall> entity, Level level)
{ {
super(entity, level); super(entity, level);
} }
public ThrownPossBall(Level level, LivingEntity shooter) public ThrownPossBall(Level level, LivingEntity shooter, ItemStack item)
{ {
super(EntityType.SNOWBALL, shooter, level); super(EntityType.SNOWBALL, shooter, level);
this.shooter = shooter; this.shooter = shooter;
} if(item.getTag() == null)
{
public ThrownPossBall(Level pLevel, double pX, double pY, double pZ) item.setTag(new CompoundTag());
{ }
super(EntityType.SNOWBALL, pX, pY, pZ, pLevel); self=item;
} }
@Override @Override
protected Item getDefaultItem() protected Item getDefaultItem() {
{
return ModItems.POSS_BALL.get(); return ModItems.POSS_BALL.get();
} }
void returnBall()
{
ItemEntity x;
if(shooter!=null)
x = new ItemEntity(getLevel(), shooter.position().x, shooter.position().y, shooter.position().z, self, 0, 0, 0);
else
x = new ItemEntity(getLevel(), position().x, position().y, position().z, self, 0, 0, 0);
getLevel().addFreshEntity(x);
}
@Override @Override
public void handleEntityEvent(byte pId) { public void handleEntityEvent(byte pId) {
if(pId == 3) if(pId == 3)
@ -70,41 +76,60 @@ public class ThrownPossBall extends ThrowableItemProjectile
@Override @Override
protected void onHitEntity(EntityHitResult pResult) { protected void onHitEntity(EntityHitResult pResult) {
super.onHitEntity(pResult); super.onHitEntity(pResult);
if(getItem().getTag() == null || !getItem().getTag().contains("entity")) if(ServerUtilities.isServer())
{ {
CompoundTag tag = self.getTag();
if(pResult.getEntity() instanceof LivingEntity le && !(le instanceof Player)) if(tag == null)tag = new CompoundTag();
if(tag.contains(ItemStack.TAG_DAMAGE))
{ {
// We don't want to capture players tag.remove(ItemStack.TAG_DAMAGE);
// Store the entity in the entity tag, then kill the entity
CompoundTag tag = new CompoundTag();
String entityName = le.getName().getString();
le.save(tag);
getItem().getTag().put("entity", tag);
getItem().setCount(1);
captured=true;
LoreContainer cont = new LoreContainer(getItem());
cont.miscData.loreData.clear();
LoreEntry entry = new LoreEntry.Builder().bold(true).text(ChatColor.doColors("!Dark_Green!Captured Mob: !Dark_Purple!" + entityName)).build();
cont.miscData.loreData.add(entry);
cont.commitLore();
le.remove(RemovalReason.DISCARDED);
} }
} else {
// Don't capture the entity
pResult.getEntity().hurt(this.shooter.getLastDamageSource().thrown(this, this.getOwner()), 0.1F); if(self.getTag() == null || !self.getTag().contains("entity"))
{
/*
Ensure the tag is not null
*/
if(pResult.getEntity() instanceof LivingEntity le && !(le instanceof Player player))
{
// We don't want to capture players
// Store the entity in the entity tag, then kill the entity
CompoundTag store = new CompoundTag();
String entityName = le.getName().getString();
le.save(store);
self.getTag().put("entity", store);
self.setCount(1);
captured=true;
LoreContainer cont = new LoreContainer(self);
cont.miscData.loreData.clear();
LoreEntry entry = new LoreEntry.Builder().bold(true).text(ChatColor.doColors("!Dark_Green!Captured Mob: !Dark_Purple!" + entityName)).build();
cont.miscData.loreData.add(entry);
cont.commitLore();
le.remove(RemovalReason.DISCARDED);
}
} else {
// Don't capture the entity
//pResult.getEntity().hurt(this.shooter.getLastDamageSource().thrown(this, this.getOwner()), 0.1F);
}
} }
} }
@Override @Override
protected void onHit(HitResult pResult) { protected void onHit(HitResult pResult) {
super.onHit(pResult); super.onHit(pResult);
if(ServerUtilities.isServer()) if(ServerUtilities.isServer())
{ {
// We do two things here // We do two things here
@ -113,9 +138,10 @@ public class ThrownPossBall extends ThrowableItemProjectile
// 2. If no entity, and none was captured, decrease the durability a little // 2. If no entity, and none was captured, decrease the durability a little
// 3. Drop the PossBall with entity, or without // 3. Drop the PossBall with entity, or without
ItemStack item = getItem(); ItemStack item = self;
CompoundTag tag = item.getTag(); CompoundTag tag = item.getTag();
if(tag == null)tag=new CompoundTag();
if(tag==null)tag=new CompoundTag();
if(tag.contains(ItemStack.TAG_DAMAGE)) if(tag.contains(ItemStack.TAG_DAMAGE))
{ {
@ -127,12 +153,7 @@ public class ThrownPossBall extends ThrowableItemProjectile
if(captured) if(captured)
{ {
// Spawn poss ball item with the entity NBT // Spawn poss ball item with the entity NBT
ItemEntity entity; returnBall();
if(shooter != null)
entity = new ItemEntity(getLevel(), shooter.position().x, shooter.position().y, shooter.position().z, item, 0, 0, 0);
else
entity = new ItemEntity(getLevel(), shooter.position().x, shooter.position().y, shooter.position().z, item, 0, 0, 0);
getLevel().addFreshEntity(entity);
} else { } else {
// Spawn the real entity // Spawn the real entity
Optional<Entity> entity = EntityType.create(tag.getCompound("entity"), getLevel()); Optional<Entity> entity = EntityType.create(tag.getCompound("entity"), getLevel());
@ -155,13 +176,8 @@ public class ThrownPossBall extends ThrowableItemProjectile
tag.remove("entity"); tag.remove("entity");
} }
ItemEntity x;
if(shooter!=null) returnBall();
x = new ItemEntity(getLevel(), shooter.position().x, shooter.position().y, shooter.position().z, item, 0, 0, 0);
else
x = new ItemEntity(getLevel(), position().x, position().y, position().z, item, 0, 0, 0);
getLevel().addFreshEntity(x);
} }
} else { } else {
// No capture // No capture
@ -175,21 +191,15 @@ public class ThrownPossBall extends ThrowableItemProjectile
if(tag.size()==0) if(tag.size()==0)
{ {
tag=null; tag=null;
item.setTag(null); item.setTag(new CompoundTag());
} }
//if(item.getDamageValue() >= item.getMaxDamage()) //if(item.getDamageValue() >= item.getMaxDamage())
// return; // return;
ItemEntity entity;
if(shooter!= null) returnBall();
entity = new ItemEntity(getLevel(),shooter.position().x, shooter.position().y, shooter.position().z, item, 0, 0, 0);
else
entity = new ItemEntity(getLevel(), position().x, position().y, position().z, item, 0, 0, 0);
getLevel().addFreshEntity(entity);
} }
this.getLevel().broadcastEntityEvent(this, (byte)3); this.getLevel().broadcastEntityEvent(this, (byte)3);

View file

@ -9,7 +9,7 @@
} }
], ],
"result": { "result": {
"item": "otemod:dirty_pool_tiles", "item": "otemod:dirty_pool_tile",
"count": 4 "count": 4
} }
} }