Skip to content

Commit

Permalink
Add "Interaction" entity to entity_spec() and set_entity_spec() (#1363)
Browse files Browse the repository at this point in the history
  • Loading branch information
VergilPrime committed Jul 21, 2023
1 parent f676632 commit e31eb47
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.laytonsmith.abstraction.bukkit.entities;

import com.laytonsmith.abstraction.entities.MCInteraction;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Interaction;
import org.bukkit.entity.Interaction.PreviousInteraction;

public class BukkitMCInteraction extends BukkitMCEntity implements MCInteraction {
Interaction interaction;

public BukkitMCInteraction(Entity i) {
super(i);
this.interaction = (Interaction) i;
}

@Override
public double getWidth() {
return interaction.getInteractionWidth();
}

@Override
public void setWidth(double width) {
interaction.setInteractionWidth((float) width);
}

@Override
public double getHeight() {
return interaction.getInteractionHeight();
}

@Override
public void setHeight(double height) {
interaction.setInteractionHeight((float) height);
}

@Override
public boolean isResponsive() {
return interaction.isResponsive();
}

@Override
public void setResponsive(boolean response) {
interaction.setResponsive(response);
}

@Override
public MCPreviousInteraction getLastAttack() {
PreviousInteraction pi = interaction.getLastAttack();
if(pi == null) {
return null;
}
return new MCPreviousInteraction(pi.getPlayer().getUniqueId(), pi.getTimestamp());
}

@Override
public MCPreviousInteraction getLastInteraction() {
PreviousInteraction pi = interaction.getLastInteraction();
if(pi == null) {
return null;
}
return new MCPreviousInteraction(pi.getPlayer().getUniqueId(), pi.getTimestamp());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.laytonsmith.abstraction.entities;

import com.laytonsmith.abstraction.MCEntity;

import java.util.UUID;

public interface MCInteraction extends MCEntity {
double getWidth();
void setWidth(double width);
double getHeight();
void setHeight(double height);
boolean isResponsive();
void setResponsive(boolean response);
MCPreviousInteraction getLastAttack();
MCPreviousInteraction getLastInteraction();

class MCPreviousInteraction {
private final UUID uuid;
private final long timestamp;

public MCPreviousInteraction(UUID uuid, long timestamp) {
this.uuid = uuid;
this.timestamp = timestamp;
}

public UUID getUuid() {
return uuid;
}

public long getTimestamp() {
return timestamp;
}
}

}
52 changes: 52 additions & 0 deletions src/main/java/com/laytonsmith/core/functions/EntityManagement.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
import com.laytonsmith.abstraction.entities.MCHorse;
import com.laytonsmith.abstraction.entities.MCHorse.MCHorseColor;
import com.laytonsmith.abstraction.entities.MCHorse.MCHorsePattern;
import com.laytonsmith.abstraction.entities.MCInteraction;
import com.laytonsmith.abstraction.entities.MCInteraction.MCPreviousInteraction;
import com.laytonsmith.abstraction.entities.MCIronGolem;
import com.laytonsmith.abstraction.entities.MCItem;
import com.laytonsmith.abstraction.entities.MCItemFrame;
Expand Down Expand Up @@ -2037,6 +2039,30 @@ public Mixed exec(Target t, Environment environment, Mixed... args) throws Confi
specArray.set(entity_spec.KEY_HORSE_ARMOR, ObjectGenerator.GetGenerator().item(horse.getArmor(), t), t);
specArray.set(entity_spec.KEY_HORSE_SADDLE, ObjectGenerator.GetGenerator().item(horse.getSaddle(), t), t);
break;
case INTERACTION:
MCInteraction interaction = (MCInteraction) entity;
specArray.set(entity_spec.KEY_INTERACTION_WIDTH, new CDouble(interaction.getWidth(), t), t);
specArray.set(entity_spec.KEY_INTERACTION_HEIGHT, new CDouble(interaction.getHeight(), t), t);
specArray.set(entity_spec.KEY_INTERACTION_RESPONSE, CBoolean.get(interaction.isResponsive()), t);
MCPreviousInteraction attack = interaction.getLastAttack();
if(attack != null) {
CArray attackArray = CArray.GetAssociativeArray(t);
attackArray.set("puuid", attack.getUuid().toString());
attackArray.set("timestamp", new CInt(attack.getTimestamp(), t), t);
specArray.set(entity_spec.KEY_INTERACTION_ATTACK, attackArray, t);
} else {
specArray.set(entity_spec.KEY_INTERACTION_ATTACK, CNull.NULL, t);
}
MCPreviousInteraction interact = interaction.getLastInteraction();
if(interact != null) {
CArray interactionArray = CArray.GetAssociativeArray(t);
interactionArray.set("puuid", interact.getUuid().toString());
interactionArray.set("timestamp", new CInt(interact.getTimestamp(), t), t);
specArray.set(entity_spec.KEY_INTERACTION_INTERACTION, interactionArray, t);
} else {
specArray.set(entity_spec.KEY_INTERACTION_INTERACTION, CNull.NULL, t);
}
break;
case IRON_GOLEM:
MCIronGolem golem = (MCIronGolem) entity;
specArray.set(entity_spec.KEY_IRON_GOLEM_PLAYERCREATED, CBoolean.get(golem.isPlayerCreated()), t);
Expand Down Expand Up @@ -2330,6 +2356,11 @@ public MSVersion since() {
private static final String KEY_HORSE_MAXDOMESTICATION = "maxdomestication";
private static final String KEY_HORSE_ARMOR = "armor";
private static final String KEY_HORSE_SADDLE = "saddle";
private static final String KEY_INTERACTION_WIDTH = "width";
private static final String KEY_INTERACTION_HEIGHT = "height";
private static final String KEY_INTERACTION_RESPONSE = "response";
private static final String KEY_INTERACTION_ATTACK = "lastattack";
private static final String KEY_INTERACTION_INTERACTION = "lastinteraction";
private static final String KEY_IRON_GOLEM_PLAYERCREATED = "playercreated";
private static final String KEY_ITEM_FRAME_FIXED = "fixed";
private static final String KEY_ITEM_FRAME_ITEM = "item";
Expand Down Expand Up @@ -3128,6 +3159,27 @@ public Mixed exec(Target t, Environment environment, Mixed... args) throws Confi
}
}
break;
case INTERACTION:
MCInteraction interaction = (MCInteraction) entity;
for(String index : specArray.stringKeySet()) {
switch(index.toLowerCase()) {
case entity_spec.KEY_INTERACTION_HEIGHT:
interaction.setHeight(ArgumentValidation.getDouble(specArray.get(index, t), t));
break;
case entity_spec.KEY_INTERACTION_WIDTH:
interaction.setWidth(ArgumentValidation.getDouble(specArray.get(index, t), t));
break;
case entity_spec.KEY_INTERACTION_RESPONSE:
interaction.setResponsive(ArgumentValidation.getBooleanish(specArray.get(index, t), t));
break;
case entity_spec.KEY_INTERACTION_ATTACK:
case entity_spec.KEY_INTERACTION_INTERACTION:
break;
default:
throwException(index, t);
}
}
break;
case IRON_GOLEM:
MCIronGolem golem = (MCIronGolem) entity;
for(String index : specArray.stringKeySet()) {
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/functionDocs/entity_spec
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ without knowing the rotations on the other axis or of other body parts beforehan
* %KEY_HORSE_SADDLE%: An item pertaining to the saddle a horse has put on. Can be anything.
* %KEY_HORSE_STYLE%: The horse's style (can be %HORSE_STYLE%).
|-
| INTERACTION
|
* %KEY_INTERACTION_WIDTH%: The width of the interaction's hit box on the X and Z axis.
* %KEY_INTERACTION_HEIGHT%: The height of the interaction's hit box on the Y axis.
* %KEY_INTERACTION_RESPONSE%: Whether vanilla responses occur such as sound and arm swing when interacting with the entity.
* %KEY_INTERACTION_ATTACK%, %KEY_INTERACTION_INTERACTION%: An associative array containing the keys 'puuid' and 'timestamp' which is updated when a player attacks or right clicks the interaction respectively. Cannot be updated with set_entity_spec().
|-
| IRON_GOLEM
|
* %KEY_IRON_GOLEM_PLAYERCREATED%: Whether the iron golem was built by a player or not.
Expand Down

0 comments on commit e31eb47

Please sign in to comment.