/
LoggedEntity.java
150 lines (128 loc) · 4.74 KB
/
LoggedEntity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* Author: Lucas Arnström - LucasEmanuel @ Bukkit forums
* Contact: lucasarnstrom(at)gmail(dot)com
*
*
* Copyright 2014 Lucas Arnström
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package se.lucasarnstrom.sgmultiverse2.logging;
import org.bukkit.Art;
import org.bukkit.Location;
import org.bukkit.Rotation;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.*;
import org.bukkit.inventory.ItemStack;
import java.util.HashMap;
public class LoggedEntity {
private final Location location;
private final EntityType type;
private final HashMap<String, Object> data = new HashMap<>();
public LoggedEntity(Entity e) {
type = e.getType();
if(e instanceof Hanging) {
Hanging h = (Hanging) e;
data.put("FacingDirection", h.getFacing());
// Get the pure location (no decimal value)
location = e.getLocation().getBlock().getLocation();
if(h instanceof Painting) {
Painting p = (Painting) h;
data.put("Art", p.getArt());
}
else if(h instanceof ItemFrame) {
ItemFrame i = (ItemFrame) h;
data.put("ItemStack", i.getItem().clone());
data.put("Rotation", i.getRotation());
}
}
else
location = e.getLocation();
}
public void reset() {
switch(type) {
case ITEM_FRAME:
BlockFace face = (BlockFace) data.get("FacingDirection");
ItemFrame i = location.getWorld().spawn(location.getBlock().getRelative(face.getOppositeFace()).getLocation(), ItemFrame.class);
i.teleport(location);
i.setRotation((Rotation) data.get("Rotation"));
i.setFacingDirection(face, true);
i.setItem((ItemStack) data.get("ItemStack"));
break;
case PAINTING:
BlockFace face2 = (BlockFace) data.get("FacingDirection");
Art art = (Art) data.get("Art");
Painting p = location.getWorld().spawn(location.getBlock().getRelative(face2.getOppositeFace()).getLocation(), Painting.class);
p.teleport(calculatePainting(art, face2, location));
p.setFacingDirection(face2, true);
p.setArt(art, true);
break;
default:
}
}
private Location calculatePainting(Art art, BlockFace facing, Location loc) {
switch(art) {
// 1x1
case ALBAN:
case AZTEC:
case AZTEC2:
case BOMB:
case KEBAB:
case PLANT:
case WASTELAND:
return loc; // No calculation needed.
// 1x2
case GRAHAM:
case WANDERER:
return loc.getBlock().getLocation().add(0, -1, 0);
// 2x1
case CREEBET:
case COURBET:
case POOL:
case SEA:
case SUNSET: // Use same as 4x3
// 4x3
case DONKEYKONG:
case SKELETON:
if(facing == BlockFace.WEST)
return loc.getBlock().getLocation().add(0, 0, -1);
else if(facing == BlockFace.SOUTH)
return loc.getBlock().getLocation().add(-1, 0, 0);
else
return loc;
// 2x2
case BUST:
case MATCH:
case SKULL_AND_ROSES:
case STAGE:
case VOID:
case WITHER: // Use same as 4x2
// 4x2
case FIGHTERS: // Use same as 4x4
// 4x4
case BURNINGSKULL:
case PIGSCENE:
case POINTER:
if(facing == BlockFace.WEST)
return loc.getBlock().getLocation().add(0, -1, -1);
else if(facing == BlockFace.SOUTH)
return loc.getBlock().getLocation().add(-1, -1, 0);
else
return loc.add(0, -1, 0);
// Unsupported artwork
default:
return loc;
}
}
}