Merge branch 'master' into 1.17
# Conflicts: # src/main/java/carpet/mixins/ServerWorld_scarpetMixin.java # src/main/java/carpet/script/api/WorldAccess.java
This commit is contained in:
commit
388a3de577
15 files changed
+326
-19
No files matched your search
@@ -5,6 +5,7 @@ import carpet.CarpetSettings;
|
||||
import carpet.helpers.TickSpeed;
|
||||
import carpet.script.exception.InternalExpressionException;
|
||||
import carpet.script.value.BlockValue;
|
||||
import carpet.script.value.BooleanValue;
|
||||
import carpet.script.value.EntityValue;
|
||||
import carpet.script.value.FunctionValue;
|
||||
import carpet.script.value.ListValue;
|
||||
@@ -19,8 +20,11 @@ import carpet.utils.Messenger;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.entity.EntityType;
|
||||
import net.minecraft.entity.LivingEntity;
|
||||
import net.minecraft.entity.TntEntity;
|
||||
import net.minecraft.entity.damage.DamageSource;
|
||||
import net.minecraft.entity.passive.MerchantEntity;
|
||||
import net.minecraft.entity.projectile.ProjectileEntity;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.command.ServerCommandSource;
|
||||
import net.minecraft.entity.Entity;
|
||||
@@ -42,6 +46,7 @@ import net.minecraft.village.Merchant;
|
||||
import net.minecraft.village.TradeOffer;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.chunk.Chunk;
|
||||
import net.minecraft.world.explosion.Explosion;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -52,6 +57,7 @@ import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
@@ -865,6 +871,59 @@ public class CarpetEventServer
|
||||
);
|
||||
}
|
||||
};
|
||||
//copy of Explosion.getCausingEntity() #TRACK#
|
||||
private static LivingEntity getExplosionCausingEntity(Entity entity)
|
||||
{
|
||||
if (entity == null) return null;
|
||||
else if (entity instanceof TntEntity) return ((TntEntity)entity).getCausingEntity();
|
||||
else if (entity instanceof LivingEntity) return (LivingEntity)entity;
|
||||
else if (entity instanceof ProjectileEntity) {
|
||||
Entity owner = ((ProjectileEntity)entity).getOwner();
|
||||
if (owner instanceof LivingEntity) return (LivingEntity)owner;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static final Event EXPLOSION_OUTCOME = new Event("explosion_outcome", 8, true)
|
||||
{
|
||||
@Override
|
||||
public void onExplosion(ServerWorld world, Entity e, Supplier<LivingEntity> attacker, double x, double y, double z, float power, boolean createFire, List<BlockPos> affectedBlocks, List<Entity> affectedEntities, Explosion.DestructionType type)
|
||||
{
|
||||
handler.call(
|
||||
() -> Arrays.asList(
|
||||
ListValue.fromTriple(x, y, z),
|
||||
NumericValue.of(power),
|
||||
EntityValue.of(e),
|
||||
EntityValue.of(attacker!= null?attacker.get():Event.getExplosionCausingEntity(e)),
|
||||
StringValue.of(type.name().toLowerCase(Locale.ROOT)),
|
||||
BooleanValue.of(createFire),
|
||||
new ListValue(affectedBlocks.stream().filter(b -> !world.isAir(b)). map( // da heck they send air blocks
|
||||
b -> new BlockValue(world.getBlockState(b),world,b)
|
||||
).collect(Collectors.toList())),
|
||||
new ListValue(affectedEntities.stream().map(EntityValue::of).collect(Collectors.toList()))
|
||||
), () -> CarpetServer.minecraft_server.getCommandSource().withWorld(world)
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
public static final Event EXPLOSION = new Event("explosion", 6, true)
|
||||
{
|
||||
@Override
|
||||
public void onExplosion(ServerWorld world, Entity e, Supplier<LivingEntity> attacker, double x, double y, double z, float power, boolean createFire, List<BlockPos> affectedBlocks, List<Entity> affectedEntities, Explosion.DestructionType type)
|
||||
{
|
||||
handler.call(
|
||||
() -> Arrays.asList(
|
||||
ListValue.fromTriple(x, y, z),
|
||||
NumericValue.of(power),
|
||||
EntityValue.of(e),
|
||||
EntityValue.of(attacker!= null?attacker.get():Event.getExplosionCausingEntity(e)),
|
||||
StringValue.of(type.name().toLowerCase(Locale.ROOT)),
|
||||
BooleanValue.of(createFire)
|
||||
), () -> CarpetServer.minecraft_server.getCommandSource().withWorld(world)
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
public static String getEntityLoadEventName(EntityType<? extends Entity> et)
|
||||
{
|
||||
@@ -985,7 +1044,7 @@ public class CarpetEventServer
|
||||
public void onSlotSwitch(ServerPlayerEntity player, int from, int to) {}
|
||||
public void onTrade(ServerPlayerEntity player, Merchant merchant, TradeOffer tradeOffer) {}
|
||||
|
||||
|
||||
public void onExplosion(ServerWorld world, Entity e, Supplier<LivingEntity> attacker, double x, double y, double z, float power, boolean createFire, List<BlockPos> affectedBlocks, List<Entity> affectedEntities, Explosion.DestructionType type) { }
|
||||
public void onWorldEvent(ServerWorld world, BlockPos pos) { }
|
||||
public void onWorldEventFlag(ServerWorld world, BlockPos pos, int flag) { }
|
||||
public void onCarpetRuleChanges(ParsedRule<?> rule, ServerCommandSource source) { }
|
||||
|
||||
@@ -50,6 +50,7 @@ import net.minecraft.enchantment.Enchantments;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.EntityType;
|
||||
import net.minecraft.entity.FallingBlockEntity;
|
||||
import net.minecraft.entity.LivingEntity;
|
||||
import net.minecraft.entity.ai.pathing.NavigationType;
|
||||
import net.minecraft.item.BlockItem;
|
||||
import net.minecraft.item.Item;
|
||||
@@ -61,6 +62,7 @@ import net.minecraft.item.SwordItem;
|
||||
import net.minecraft.item.TridentItem;
|
||||
import net.minecraft.nbt.NbtCompound;
|
||||
import net.minecraft.nbt.NbtElement;
|
||||
import net.minecraft.network.packet.s2c.play.ExplosionS2CPacket;
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
import net.minecraft.server.world.ChunkTicket;
|
||||
import net.minecraft.server.world.ChunkTicketType;
|
||||
@@ -80,6 +82,7 @@ import net.minecraft.util.math.BlockBox;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.math.ChunkPos;
|
||||
import net.minecraft.util.math.Direction;
|
||||
import net.minecraft.util.math.Vec3d;
|
||||
import net.minecraft.util.registry.Registry;
|
||||
import net.minecraft.world.Heightmap;
|
||||
import net.minecraft.world.LightType;
|
||||
@@ -89,6 +92,7 @@ import net.minecraft.world.biome.Biome;
|
||||
import net.minecraft.world.chunk.Chunk;
|
||||
import net.minecraft.world.chunk.ChunkStatus;
|
||||
import net.minecraft.world.chunk.WorldChunk;
|
||||
import net.minecraft.world.explosion.Explosion;
|
||||
import net.minecraft.world.gen.ChunkRandom;
|
||||
import net.minecraft.world.gen.chunk.ChunkGenerator;
|
||||
import net.minecraft.world.gen.feature.ConfiguredStructureFeature;
|
||||
@@ -97,6 +101,7 @@ import net.minecraft.world.level.ServerWorldProperties;
|
||||
import net.minecraft.world.poi.PointOfInterest;
|
||||
import net.minecraft.world.poi.PointOfInterestStorage;
|
||||
import net.minecraft.world.poi.PointOfInterestType;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@@ -971,6 +976,88 @@ public class WorldAccess {
|
||||
return success ? LazyValue.TRUE : LazyValue.FALSE;
|
||||
});
|
||||
|
||||
expression.addLazyFunction("create_explosion", -1, (c, t, lv) ->
|
||||
{
|
||||
if (lv.isEmpty())
|
||||
throw new InternalExpressionException("'create_explosion' requires at least a position to explode");
|
||||
CarpetContext cc = (CarpetContext)c;
|
||||
float powah = 4.0f;
|
||||
Explosion.DestructionType mode = Explosion.DestructionType.BREAK;
|
||||
boolean createFire = false;
|
||||
Entity source = null;
|
||||
LivingEntity attacker = null;
|
||||
Vector3Argument location = Vector3Argument.findIn(cc, lv, 0, false, true);
|
||||
Vec3d pos = location.vec;
|
||||
if (lv.size() > location.offset)
|
||||
{
|
||||
powah = NumericValue.asNumber(lv.get(location.offset).evalValue(c), "explosion power").getFloat();
|
||||
if (powah < 0) throw new InternalExpressionException("Explosion power cannot be negative");
|
||||
if (lv.size() > location.offset+1)
|
||||
{
|
||||
String strval = lv.get(location.offset+1).evalValue(c).getString();
|
||||
try {
|
||||
mode = Explosion.DestructionType.valueOf(strval.toUpperCase(Locale.ROOT));
|
||||
}
|
||||
catch (IllegalArgumentException ile) { throw new InternalExpressionException("Illegal explosions block behaviour: "+strval); }
|
||||
if (lv.size() > location.offset+2)
|
||||
{
|
||||
createFire = lv.get(location.offset+2).evalValue(c, Context.BOOLEAN).getBoolean();
|
||||
if (lv.size() > location.offset+3)
|
||||
{
|
||||
Value enVal= lv.get(location.offset+3).evalValue(c);
|
||||
if (enVal.isNull()) {} // is null already
|
||||
else if (enVal instanceof EntityValue)
|
||||
{
|
||||
source = ((EntityValue) enVal).getEntity();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InternalExpressionException("Fourth parameter of the explosion has to be an entity, not "+enVal.getTypeString());
|
||||
}
|
||||
if (lv.size() > location.offset+4)
|
||||
{
|
||||
enVal = lv.get(location.offset+4).evalValue(c);
|
||||
if (enVal.isNull()) {} // is null already
|
||||
else if (enVal instanceof EntityValue)
|
||||
{
|
||||
Entity attackingEntity = ((EntityValue) enVal).getEntity();
|
||||
if (attackingEntity instanceof LivingEntity)
|
||||
{
|
||||
attacker = (LivingEntity) attackingEntity;
|
||||
}
|
||||
else throw new InternalExpressionException("Attacking entity needs to be a living thing, "+
|
||||
ValueConversions.of(Registry.ENTITY_TYPE.getId(attackingEntity.getType())).getString() +" ain't it.");
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InternalExpressionException("Fifth parameter of the explosion has to be a living entity, not "+enVal.getTypeString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
LivingEntity theAttacker = attacker;
|
||||
float thePowah = powah;
|
||||
|
||||
// copy of ServerWorld.createExplosion #TRACK#
|
||||
Explosion explosion = new Explosion(cc.s.getWorld(), source, null, null, pos.x, pos.y, pos.z, powah, createFire, mode){
|
||||
@Override
|
||||
public @Nullable LivingEntity getCausingEntity() {
|
||||
return theAttacker;
|
||||
}
|
||||
};
|
||||
explosion.collectBlocksAndDamageEntities();
|
||||
explosion.affectWorld(false);
|
||||
if (mode == Explosion.DestructionType.NONE) explosion.clearAffectedBlocks();
|
||||
cc.s.getWorld().getPlayers().forEach(spe -> {
|
||||
if (spe.squaredDistanceTo(pos) < 4096.0D)
|
||||
spe.networkHandler.sendPacket(new ExplosionS2CPacket(pos.x, pos.y, pos.z, thePowah, explosion.getAffectedBlocks(), explosion.getAffectedPlayers().get(spe)));
|
||||
});
|
||||
return LazyValue.TRUE;
|
||||
});
|
||||
|
||||
// TODO rename to use_item
|
||||
expression.addLazyFunction("place_item", -1, (c, t, lv) ->
|
||||
{
|
||||
|
||||
@@ -7,6 +7,7 @@ import carpet.script.Tokenizer;
|
||||
import carpet.script.value.FunctionValue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@@ -26,10 +27,7 @@ public class ExpressionException extends RuntimeException implements ResolvedExc
|
||||
|
||||
public ExpressionException(Context c, Expression e, Tokenizer.Token t, String message)
|
||||
{
|
||||
super("Error");
|
||||
lazyMessage = () -> makeMessage(c, e, t, message);
|
||||
token = t;
|
||||
context = c;
|
||||
this(c, e, t, message, Collections.emptyList());
|
||||
}
|
||||
public ExpressionException(Context c, Expression e, Tokenizer.Token t, String message, List<FunctionValue> stack)
|
||||
{
|
||||
|
||||
@@ -101,6 +101,12 @@ public class EntityValue extends Value
|
||||
entity = e;
|
||||
}
|
||||
|
||||
public static Value of(Entity e)
|
||||
{
|
||||
if (e == null) return Value.NULL;
|
||||
return new EntityValue(e);
|
||||
}
|
||||
|
||||
private static final Map<String, EntitySelector> selectorCache = new HashMap<>();
|
||||
public static Collection<? extends Entity > getEntitiesFromSelector(ServerCommandSource source, String selector)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user